package cn.stylefeng.guns.modular.transport.service.impl;

import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import cn.hutool.core.collection.ListUtil;
import cn.stylefeng.guns.modular.basic.entity.*;
import cn.stylefeng.guns.modular.basic.model.params.*;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicLoadingQuotationResult;
import cn.stylefeng.guns.modular.basic.service.*;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetails;
import cn.stylefeng.guns.modular.fi.mapper.FiFreightDetailsLogMapper;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsLogService;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsService;
import cn.stylefeng.guns.modular.location.excel.ExcelUtils;
import cn.stylefeng.guns.modular.transport.entity.*;
import cn.stylefeng.guns.modular.transport.mapper.TDispatchMapper;
import cn.stylefeng.guns.modular.transport.mapper.TransportMsgMapper;
import cn.stylefeng.guns.modular.transport.mapper.TransportPlateStoreMapper;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.result.BasicLinePriceResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPrivatecarResult;
import cn.stylefeng.guns.modular.hx.basic.entity.HxBasicPositionGoods;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageOutDetailedResult;
import cn.stylefeng.guns.modular.order.entity.OrderTransport;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportDetailedParam;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportDetailedResult;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportResult;
import cn.stylefeng.guns.modular.order.service.OrderTransportDetailedService;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.order.service.OutOrderService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageOutResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutService;
import cn.stylefeng.guns.modular.transport.mapper.TransportDispatchMapper;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportCostParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportMsgParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportReceiptParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportStevedoreSigninParam;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportDispatchResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportMsgResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportReceiptResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportStevedoreSigninResult;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportCostService;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportMsgService;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.guns.modular.transport.service.TransportStevedoreSigninService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 调度单表 服务实现类
 */
@Service
public class TransportDispatchServiceImpl extends ServiceImpl<TransportDispatchMapper, TransportDispatch> implements TransportDispatchService {

    @Resource
    private UserService userService;

    @Resource
    private OrderTransportService orderTransportService;

    @Resource
    private BasicGoodsService basicGoodsService;

    @Resource
    private OrderTransportDetailedService orderTransportDetailedService;

    @Resource
    private BasicPrivatecarService basicPrivatecarService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Resource
    private TransportDispatchService transportDispatchService;

    @Autowired
    private BasicLoadingQuotationService basicLoadingQuotationService;

    @Resource
    private OutOrderService outOrderService;

    @Resource
    private StorageOutService storageOutService;

    @Resource
    private BasicOperateService basicOperateService;

    @Resource
    private TransportPlateStoreMapper transportPlateStoreMapper;

    @Resource
    private TransportMsgMapper transportMsgMapper;


    @Override
    public void add(TransportDispatchParam param) {
        TransportDispatch entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TransportDispatchParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(TransportDispatchParam param) {
        TransportDispatch oldEntity = getOldEntity(param);
        TransportDispatch newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TransportDispatchResult findBySpec(TransportDispatchParam param) {
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public TransportDispatchResult findBySpecNotEqId(TransportDispatchParam param) {
        return this.baseMapper.findBySpecNotEqId(param);
    }

    @Override
    public List<TransportDispatchResult> findCarListBySpec(TransportDispatchParam param, String getUnit, String getLocation, String token, String carState) {
        return this.baseMapper.findCarListBySpec(param, getUnit, getLocation, token, carState);
    }

    @Override
    public List<TransportDispatchResult> findAutoPrintByDispatchNumber(String dispatchNumber) {
        return this.baseMapper.findAutoPrintByDispatchNumber(dispatchNumber);
    }


    @Override
    public LayuiPageInfo findPageBySpec(TransportDispatchParam param, String condition, TransportReceiptParam param2) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param, condition, param2);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPpPageBySpec(TransportDispatchParam param, String condition, Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPpPageBySpec(pageContext, param, condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TransportDispatchParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TransportDispatch getOldEntity(TransportDispatchParam param) {
        return this.getById(getKey(param));
    }

    private TransportDispatch getEntity(TransportDispatchParam param) {
        TransportDispatch entity = new TransportDispatch();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<TransportDispatchResult> findDispatchListBySpec(TransportDispatchParam param, String token) {
        return this.baseMapper.findDispatchListBySpec(param, token);
    }

    @Override
    public List<TransportDispatchResult> findgoodslistBySpec(TransportDispatchParam param, String getUnit, String getLocation, String token, String carState) {
        return this.baseMapper.findgoodslistBySpec(param, getUnit, getLocation, token, carState);
    }

    /**
     * 修改派车 明细
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData editGoods(TransportDispatchParam transportDispatchParam, ResponseData data) {
        User user = userAop.getUser();
        TransportDispatch transportDispatch = transportDispatchService.getById(transportDispatchParam.getId());//原派车明细

        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(transportDispatch.getDispatchNumber());
        tDispatchParam.setDeptId(user.getDeptId());
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, user.getOperateId());//派车单
        if (tDispatchResult != null) {
            if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("已发运无法修改");
            }
        }

        OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
        otp.setTransportNumber(transportDispatch.getTransportNumber());
        OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

        OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();
        otdp.setGoodId(transportDispatch.getGoodsId());
        otdp.setTransportNumber(transportDispatch.getTransportNumber());
        otdp.setDetailedDate(transportDispatch.getDetailedDate());
        if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
            if (otr.getOrderSource().equals("零脉")) {
                otdp.setPositionId(transportDispatch.getPositionId());
            }
        }
        OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

        BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
        BigDecimal num = new BigDecimal(transportDispatchParam.getTooDeployNum());//分配数量
        BigDecimal volume = num.multiply(goodsVolume).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配体积
        BigDecimal jibennum = new BigDecimal("0");
        if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
            if (otr.getOrderSource().equals("零脉")) {
                BigDecimal ratebig = new BigDecimal(otdr.getRate());
                jibennum = num.multiply(ratebig).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配基本数量
            }
        }
        BigDecimal as = new BigDecimal(0);

        BigDecimal tooDeployNum = new BigDecimal(transportDispatch.getTooDeployNum());
        BigDecimal tooDeployVolume = new BigDecimal(transportDispatch.getTooDeployVolume());
        if (tooDeployNum.compareTo(num) == -1) {//增加
            BigDecimal volumes = volume.subtract(tooDeployVolume);//新增加体积
            BigDecimal nums = num.subtract(tooDeployNum);//新增加数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            transportDispatch.setJibenNum(jibennum + "");
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.subtract(volumes);//未分配体积 - 新分配体积 = 新未分配体积
            BigDecimal newnotnum = notnum.subtract(nums);//未分配数量 - 新分配数量 = 新未分配数量
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }

            //修改订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.add(volumes);//已分配体积 + 新分配体积
            BigDecimal ntooNum = tooNum.add(nums);//已分配数量 + 新分配数量
            BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
            if (ntooNum.compareTo(totalNum) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntooNum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);
        } else if (tooDeployNum.compareTo(num) == 1) {//减少
            BigDecimal volumec = tooDeployVolume.subtract(volume);//减少体积
            BigDecimal numc = tooDeployNum.subtract(num);//减少数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            transportDispatch.setJibenNum(jibennum + "");
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(volumec);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(numc);//未分配数量 + 减少数量 = 新未分配数量
            BigDecimal numd = new BigDecimal(otdr.getNum());
            if (newnotnum.compareTo(numd) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }

            //修改货物订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(volumec);//已分配体积 - 减少体积
            BigDecimal ntooNum = tooNum.subtract(numc);//已分配数量 + 新分配数量
            otr.setTooVolume(ntoovolume.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);
            if (ntooNum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntooNum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);
        }
        return ResponseData.success();
    }

    /**
     * 修改派车 明细
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData editGoodsWeb(TransportDispatchParam transportDispatchParam, ResponseData data) {
        User user = userService.getcurrentUser();
        TransportDispatch transportDispatch = transportDispatchService.getById(transportDispatchParam.getId());//原派车明细

        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(transportDispatch.getDispatchNumber());
        tDispatchParam.setDeptId(user.getDeptId());
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, user.getOperateId());//派车单
        if (tDispatchResult != null) {
            if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("已发运无法修改");
            }
        }

        OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();
        otdp.setGoodId(transportDispatch.getGoodsId());
        otdp.setTransportNumber(transportDispatch.getTransportNumber());
        OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

        OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
        otp.setTransportNumber(otdr.getTransportNumber());
        OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

        BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
        BigDecimal num = new BigDecimal(transportDispatchParam.getTooDeployNum());//分配数量
        BigDecimal volume = num.multiply(goodsVolume).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配体积
        BigDecimal as = new BigDecimal(0);

        BigDecimal tooDeployNum = new BigDecimal(transportDispatch.getTooDeployNum());
        BigDecimal tooDeployVolume = new BigDecimal(transportDispatch.getTooDeployVolume());
        if (tooDeployNum.compareTo(num) == -1) {//增加
            BigDecimal volumes = volume.subtract(tooDeployVolume);//新增加体积
            BigDecimal nums = num.subtract(tooDeployNum);//新增加数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.subtract(volumes);//未分配体积 - 新分配体积 = 新未分配体积
            BigDecimal newnotnum = notnum.subtract(nums);//未分配数量 - 新分配数量 = 新未分配数量
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }

            //修改订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.add(volumes);//已分配体积 + 新分配体积
            BigDecimal ntooNum = tooNum.add(nums);//已分配数量 + 新分配数量
            BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
            if (ntooNum.compareTo(totalNum) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntooNum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);
        } else if (tooDeployNum.compareTo(num) == 1) {//减少
            BigDecimal volumec = tooDeployVolume.subtract(volume);//减少体积
            BigDecimal numc = tooDeployNum.subtract(num);//减少数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(volumec);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(numc);//未分配数量 + 减少数量 = 新未分配数量
            BigDecimal numd = new BigDecimal(otdr.getNum());
            if (newnotnum.compareTo(numd) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }

            //修改货物订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(volumec);//已分配体积 - 减少体积
            BigDecimal ntooNum = tooNum.subtract(numc);//已分配数量 + 新分配数量
            otr.setTooVolume(ntoovolume.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);
            if (ntooNum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntooNum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);
        }
        return ResponseData.success();
    }

    /**
     * 调整 派车单 商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 60)
    public ResponseData tiaozhengGoodsDispatch(TransportDispatchParam transportDispatchParam, ResponseData data, User user) {
        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(transportDispatchParam.getDispatchNumber());
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);//派车单

        TransportDispatch transportDispatch = transportDispatchService.getById(transportDispatchParam.getId());//原派车明细

        OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
        otp.setTransportNumber(transportDispatch.getTransportNumber());
        OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

        OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();
        otdp.setGoodId(transportDispatch.getGoodsId());
        otdp.setTransportNumber(transportDispatch.getTransportNumber());
        otdp.setBatchnumber(transportDispatch.getBatchnumber());
        otdp.setDetailedDate(transportDispatch.getDetailedDate());
        if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
            if (otr.getOrderSource().equals("零脉")) {
                otdp.setPositionId(transportDispatch.getPositionId());
            }
        }
        OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

        BigDecimal as = new BigDecimal(0);
        BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
        BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
        BigDecimal ynum = new BigDecimal(otdr.getNum());//数量
        BigDecimal num = new BigDecimal(transportDispatchParam.getTooDeployNum());//分配数量
        BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
        BigDecimal transportMoney = num.multiply(unitprice).setScale(2, BigDecimal.ROUND_HALF_DOWN);//分配体积
        if (ynum.compareTo(num) == 0) {
            volume = new BigDecimal(otdr.getTransportPutvolume());
            transportMoney = new BigDecimal(otdr.getTransportMoney());
        }

        BigDecimal tooDeployNum = new BigDecimal(transportDispatch.getTooDeployNum());
        BigDecimal tooDeployVolume = new BigDecimal(transportDispatch.getTooDeployVolume());
        if (num.compareTo(as) != 1) {
            return ResponseData.error("数量必须大于0");
        }
        if (tooDeployNum.compareTo(num) == -1) {//增加
            BigDecimal volumes = volume.subtract(tooDeployVolume);//新增加体积
            BigDecimal nums = num.subtract(tooDeployNum);//新增加数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                    if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                        if (otr.getDemolition().equals("拆零")) {
                            BigDecimal xianggui = new BigDecimal("10");
                            BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            transportDispatch.setMatrixingNumber(num.toString());
                        }
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                    if (otr.getOrderSource().equals("零脉")) {
                        BigDecimal ratebig = new BigDecimal(otdr.getRate());
                        BigDecimal jibenNum = num.multiply(ratebig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setJibenNum(jibenNum + "");
                    }
                }
                if (otr.getOrderSource().equals("WMS")) {
                    BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                    if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                        BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                        BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                }
            }
            transportDispatch.setTransportMoney(transportMoney.toString());
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.subtract(volumes);//未分配体积 - 新分配体积 = 新未分配体积
            BigDecimal newnotnum = notnum.subtract(nums);//未分配数量 - 新分配数量 = 新未分配数量
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();
                otdpupdate.setId(otdr.getId());
                otdpupdate.setNotnum(newnotnum.toString());
                otdpupdate.setNotvolume(newnotvolume.toString());
                orderTransportDetailedService.update(otdpupdate);
            }

            //修改订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.add(volumes);//已分配体积 + 新分配体积
            BigDecimal ntooNum = tooNum.add(nums);//已分配数量 + 新分配数量
            BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
            if (ntooNum.compareTo(totalNum) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            if (totalNum.compareTo(ntooNum) == 0) {
                //判断 配了几个车
                boolean flag = basicPrivatecarService.findTwoByOrder(otp, null);
                if (!flag) {
                    otp.setTstate("已发运");
                }
            }
            otp.setId(otr.getId());
            otp.setTooVolume(ntoovolume.toString());
            otp.setTooNum(ntooNum.toString());
            orderTransportService.update(otp);
        } else if (tooDeployNum.compareTo(num) == 1) {//减少
            BigDecimal volumec = tooDeployVolume.subtract(volume);//减少体积
            BigDecimal numc = tooDeployNum.subtract(num);//减少数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                    if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                        if (otr.getDemolition().equals("拆零")) {
                            BigDecimal xianggui = new BigDecimal("10");
                            BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            transportDispatch.setMatrixingNumber(num.toString());
                        }
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                    if (otr.getOrderSource().equals("零脉")) {
                        BigDecimal ratebig = new BigDecimal(otdr.getRate());
                        BigDecimal jibenNum = num.multiply(ratebig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setJibenNum(jibenNum + "");
                    }
                }
                if (otr.getOrderSource().equals("WMS")) {
                    BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                    if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                        BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                        BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                }
            }
            transportDispatch.setTransportMoney(transportMoney.toString());
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(volumec);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(numc);//未分配数量 + 减少数量 = 新未分配数量
            BigDecimal numd = new BigDecimal(otdr.getNum());
            if (newnotnum.compareTo(numd) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();
                otdpupdate.setId(otdr.getId());
                otdpupdate.setNotnum(newnotnum.toString());
                otdpupdate.setNotvolume(newnotvolume.toString());
                orderTransportDetailedService.update(otdpupdate);
            }

            //修改货物订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(volumec);//已分配体积 - 减少体积
            BigDecimal ntooNum = tooNum.subtract(numc);//已分配数量 + 新分配数量
            if (ntooNum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            otp.setId(otr.getId());
            otp.setTstate("待调");
            otp.setTooVolume(ntoovolume.toString());
            otp.setTooNum(ntooNum.toString());
            orderTransportService.update(otp);
        }
        if (tooDeployNum.compareTo(num) != 0) {
            TransportReceiptParam rparam = new TransportReceiptParam();
            rparam.setDispatchNumber(transportDispatchParam.getDispatchNumber());
            rparam.setGetUnitid(otr.getGetUnitId());
            TransportReceiptResult resr = transportReceiptService.findBySpec(rparam);
            if (resr != null) {
                //修改回传单
                OrderTransportParam oParam = new OrderTransportParam();
                oParam.setDeptId(user.getDeptId());
                oParam.setOrderNumber(transportDispatchParam.getDispatchNumber());
                oParam.setGetUnitId(otr.getGetUnitId());
                OrderTransportResult orderTransportResult = orderTransportService.carDetailsSingleToo1(oParam, transportDispatchParam.getCarId(), user.getOperateId());
                if (orderTransportResult != null) {
                    rparam.setId(resr.getId());
                    rparam.setNum(orderTransportResult.getTotalNum());
                    rparam.setVolume(orderTransportResult.getTooVolume());
                    transportReceiptService.update(rparam);
                }
            }

            TransportDispatchParam transportDispatchParam2 = new TransportDispatchParam();
            transportDispatchParam2.setDispatchNumber(transportDispatchParam.getDispatchNumber());
            List<TransportDispatchResult> dlist = transportDispatchService.findListBySpec(transportDispatchParam2);
            BigDecimal toonum1 = new BigDecimal(0);
            BigDecimal toovolume1 = new BigDecimal(0);
            BigDecimal toomoney1 = new BigDecimal(0);

            for (TransportDispatchResult dr : dlist) {
                //合计数量体积
                BigDecimal tooDeployNum1 = new BigDecimal(dr.getTooDeployNum());
                BigDecimal tooDeployVolume1 = new BigDecimal(dr.getTooDeployVolume());
                BigDecimal transportMoney1 = new BigDecimal(dr.getTransportMoney());
                toonum1 = toonum1.add(tooDeployNum1).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                toovolume1 = toovolume1.add(tooDeployVolume1).setScale(4, BigDecimal.ROUND_HALF_DOWN);
                toomoney1 = toomoney1.add(transportMoney1).setScale(3, BigDecimal.ROUND_HALF_DOWN);

                //查询货运订单
                OrderTransportParam orderTransportParam = new OrderTransportParam();
                orderTransportParam.setTransportNumber(dr.getTransportNumber());
                OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

                if ("待调".equals(orderTransportResult.getTstate())) {
                    BigDecimal TotalNum = new BigDecimal(orderTransportResult.getTotalNum());
                    BigDecimal TootalNum = new BigDecimal(0);
                    TransportDispatchParam param = new TransportDispatchParam();
                    param.setTransportNumber(orderTransportResult.getTransportNumber());
                    List<TransportDispatchResult> list = transportDispatchService.findDispatchListBySpec(param, null);
                    for (TransportDispatchResult dispatchResult : list) {
                        BigDecimal num2 = new BigDecimal(dispatchResult.getTooDeployNum());
                        TootalNum = TootalNum.add(num2);
                    }
                    if (TotalNum.compareTo(TootalNum) == 0) {//判断商品是否  已经全部配置
                        //判断 配了几个车
                        boolean flag = basicPrivatecarService.findTwoByOrder(orderTransportParam, null);
                        if (!flag) {
                            orderTransportParam.setId(orderTransportResult.getId());
                            orderTransportParam.setTstate("已发运");
                            orderTransportService.update(orderTransportParam);
                        }
                    }
                }
            }

            //计算满载率
            BigDecimal arr = new BigDecimal(100);
            BigDecimal privatecarSurevolume = new BigDecimal(tDispatchResult.getZhunzailiang());//准载体积
            BigDecimal fulllv = toovolume1.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
            tDispatchParam.setFulllv(fulllv.toString());

            //更新运输单
            tDispatchParam.setTooNum(toonum1.toString());
            tDispatchParam.setToomoney(toomoney1.toString());
            tDispatchParam.setToovolume(toovolume1.toString());
            tDispatchParam.setId(tDispatchResult.getId());
            tDispatchService.update(tDispatchParam);
        }
        return ResponseData.success();
    }

    /**
     * 修改 派车单 商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 60)
    public ResponseData editGoodsDispatch(TransportDispatchParam transportDispatchParam, ResponseData data, User user) {
        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(transportDispatchParam.getDispatchNumber());
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);//派车单
        if (tDispatchResult != null) {
            if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                return ResponseData.error("已发运无法修改");
            }
        }

        TransportDispatch transportDispatch = transportDispatchService.getById(transportDispatchParam.getId());//原派车明细

        OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
        otp.setTransportNumber(transportDispatch.getTransportNumber());
        OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

        OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();
        otdp.setGoodId(transportDispatch.getGoodsId());
        otdp.setTransportNumber(transportDispatch.getTransportNumber());
        otdp.setBatchnumber(transportDispatch.getBatchnumber());
        otdp.setDetailedDate(transportDispatch.getDetailedDate());
        if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
            if (otr.getOrderSource().equals("零脉")) {
                otdp.setPositionId(transportDispatch.getPositionId());
            }
        }
        OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

        BigDecimal as = new BigDecimal(0);
        BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
        BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
        BigDecimal ynum = new BigDecimal(otdr.getNum());//数量
        BigDecimal num = new BigDecimal(transportDispatchParam.getTooDeployNum());//分配数量
        BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
        BigDecimal transportMoney = num.multiply(unitprice).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配体积
        if (ynum.compareTo(num) == 0) {
            volume = new BigDecimal(otdr.getTransportPutvolume());
            transportMoney = new BigDecimal(otdr.getTransportMoney());
        }

        BigDecimal tooDeployNum = new BigDecimal(transportDispatch.getTooDeployNum());
        BigDecimal tooDeployVolume = new BigDecimal(transportDispatch.getTooDeployVolume());
        if (tooDeployNum.compareTo(num) == -1) {//增加
            BigDecimal volumes = volume.subtract(tooDeployVolume);//新增加体积
            BigDecimal nums = num.subtract(tooDeployNum);//新增加数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                    if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                        if (otr.getDemolition().equals("拆零")) {
                            BigDecimal xianggui = new BigDecimal("10");
                            BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            transportDispatch.setMatrixingNumber(num.toString());
                        }
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                    if (otr.getOrderSource().equals("零脉")) {
                        BigDecimal ratebig = new BigDecimal(otdr.getRate());
                        BigDecimal jibenNum = num.multiply(ratebig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setJibenNum(jibenNum + "");
                    }
                }
                if (otr.getOrderSource().equals("WMS")) {
                    BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                    if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                        BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                        BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                }
            }
            transportDispatch.setTransportMoney(transportMoney.toString());
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.subtract(volumes);//未分配体积 - 新分配体积 = 新未分配体积
            BigDecimal newnotnum = notnum.subtract(nums);//未分配数量 - 新分配数量 = 新未分配数量
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();
                otdpupdate.setId(otdr.getId());
                otdpupdate.setNotnum(newnotnum.toString());
                otdpupdate.setNotvolume(newnotvolume.toString());
                orderTransportDetailedService.update(otdpupdate);
            }

            //修改订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.add(volumes);//已分配体积 + 新分配体积
            BigDecimal ntooNum = tooNum.add(nums);//已分配数量 + 新分配数量
            BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
            if (ntooNum.compareTo(totalNum) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            if (totalNum.compareTo(ntooNum) == 0) {
                //判断 配了几个车
                boolean flag = basicPrivatecarService.findTwoByOrder(otp, null);
                if (!flag) {
                    otp.setTstate("已发运");
                }
            }
            otp.setId(otr.getId());
            otp.setTooVolume(ntoovolume.toString());
            otp.setTooNum(ntooNum.toString());
            orderTransportService.update(otp);
        } else if (tooDeployNum.compareTo(num) == 1) {//减少
            BigDecimal volumec = tooDeployVolume.subtract(volume);//减少体积
            BigDecimal numc = tooDeployNum.subtract(num);//减少数量
            //修改调度明细
            transportDispatch.setTooDeployVolume(volume.toString());
            transportDispatch.setTooDeployNum(num.toString());
            if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                    if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                        if (otr.getDemolition().equals("拆零")) {
                            BigDecimal xianggui = new BigDecimal("10");
                            BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            transportDispatch.setMatrixingNumber(num.toString());
                        }
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                    if (otr.getOrderSource().equals("零脉")) {
                        BigDecimal ratebig = new BigDecimal(otdr.getRate());
                        BigDecimal jibenNum = num.multiply(ratebig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setJibenNum(jibenNum + "");
                    }
                }
                if (otr.getOrderSource().equals("WMS")) {
                    BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                    if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                        BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                        BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                        transportDispatch.setMatrixingNumber(matrixingNumber.toString());
                    } else {
                        transportDispatch.setMatrixingNumber(num.toString());
                    }
                }
            }
            transportDispatch.setTransportMoney(transportMoney.toString());
            ToolUtil.copyProperties(transportDispatch, transportDispatchParam);
            transportDispatchService.update(transportDispatchParam);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(volumec);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(numc);//未分配数量 + 减少数量 = 新未分配数量
            BigDecimal numd = new BigDecimal(otdr.getNum());
            if (newnotnum.compareTo(numd) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            } else {
                OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();
                otdpupdate.setId(otdr.getId());
                otdpupdate.setNotnum(newnotnum.toString());
                otdpupdate.setNotvolume(newnotvolume.toString());
                orderTransportDetailedService.update(otdpupdate);
            }

            //修改货物订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal tooNum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(volumec);//已分配体积 - 减少体积
            BigDecimal ntooNum = tooNum.subtract(numc);//已分配数量 + 新分配数量
            if (ntooNum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            otp.setId(otr.getId());
            otp.setTstate("待调");
            otp.setTooVolume(ntoovolume.toString());
            otp.setTooNum(ntooNum.toString());
            orderTransportService.update(otp);
        }
        if (tooDeployNum.compareTo(num) != 0) {
            TransportReceiptParam receiptParam = new TransportReceiptParam();
            receiptParam.setDispatchNumber(transportDispatchParam.getDispatchNumber());
            transportReceiptService.deleteBySpec(receiptParam);//删除回传表信息

            //添加回传单
            OrderTransportParam oParam = new OrderTransportParam();
            oParam.setDeptId(user.getDeptId());
            oParam.setOrderNumber(transportDispatchParam.getDispatchNumber());
            List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo(oParam, transportDispatchParam.getCarId(), null, user.getOperateId());
            int deliverys = 1;
            String getUnits = "";
            for (int i = 0; i < list2.size(); i++) {
                OrderTransportResult orderTransportResult = list2.get(i);
                TransportReceiptParam param = new TransportReceiptParam();
                param.setDispatchNumber(transportDispatchParam.getDispatchNumber());
                param.setCarnumber(tDispatchResult.getCarnumber());
                param.setCarId(tDispatchResult.getCarId());
                param.setSetUnit(orderTransportResult.getSetUnit());
                param.setSetLocation(orderTransportResult.getSetLocation());
                param.setGetPeople(orderTransportResult.getGetPeople());
                param.setGetTel(orderTransportResult.getGetTel());
                param.setGetUnitid(orderTransportResult.getGetUnitId());
                param.setGetUnit(orderTransportResult.getGetUnit());
                getUnits += orderTransportResult.getGetUnit();
                if ((i + 1) != list2.size()) {
                    getUnits += "+";
                }
                param.setGetLocation(orderTransportResult.getGetLocation());
                param.setGetLocationid(orderTransportResult.getGetLocationId());
                param.setSdtstate("未送达");
                param.setTstate("未回传");
                param.setNum(orderTransportResult.getTotalNum());
                param.setVolume(orderTransportResult.getTooVolume());
                param.setDeptId(user.getDeptId());
                param.setDeliverId(user.getDeliverId());
                param.setSetUnitId(orderTransportResult.getSetUnitId());
                param.setSetLocationId(orderTransportResult.getSetLocationId());
                param.setStockPort(orderTransportResult.getStockPort());
                param.setDeliverys(deliverys + "");
                param.setXlbClientId(orderTransportResult.getXlbClientId());
                deliverys++;
                transportReceiptService.add(param);
            }
            if (getUnits.length() < 300) {
                tDispatchParam.setGetUnits(getUnits);
            }

            TransportDispatchParam transportDispatchParam2 = new TransportDispatchParam();
            transportDispatchParam2.setDispatchNumber(transportDispatchParam.getDispatchNumber());
            List<TransportDispatchResult> dlist = transportDispatchService.findListBySpec(transportDispatchParam2);
            BigDecimal toonum1 = new BigDecimal(0);
            BigDecimal toovolume1 = new BigDecimal(0);
            BigDecimal toomoney1 = new BigDecimal(0);

            String transportNumber = "";
            String customerOrder = "";
            List<String> line = new ArrayList<String>();
            for (TransportDispatchResult dr : dlist) {
                //合计数量体积
                BigDecimal tooDeployNum1 = new BigDecimal(dr.getTooDeployNum());
                BigDecimal tooDeployVolume1 = new BigDecimal(dr.getTooDeployVolume());
                BigDecimal transportMoney1 = new BigDecimal(dr.getTransportMoney());
                toonum1 = toonum1.add(tooDeployNum1).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                toovolume1 = toovolume1.add(tooDeployVolume1).setScale(4, BigDecimal.ROUND_HALF_DOWN);
                toomoney1 = toomoney1.add(transportMoney1).setScale(3, BigDecimal.ROUND_HALF_DOWN);

                //查询货运订单
                OrderTransportParam orderTransportParam = new OrderTransportParam();
                orderTransportParam.setTransportNumber(dr.getTransportNumber());
                OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

                //新增运输费用
                if (orderTransportResult.getLineId() != null) {
                    BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                    if (basicLine != null) {
                        if (basicLine.getStatus().equals("1")) {
                            boolean yesflag = false;
                            for (String str : line) {
                                if (str.equals(basicLine.getLineName())) {
                                    yesflag = true;
                                    break;
                                }
                            }
                            if (!yesflag) {
                                line.add(basicLine.getLineName());
                            }
                        }
                    }
                }

                if ("待调".equals(orderTransportResult.getTstate())) {
                    if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                        transportNumber += orderTransportResult.getTransportNumber() + ";";
                    }
                    if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                        customerOrder += orderTransportResult.getCustomerOrder() + ";";
                    }

                    BigDecimal TotalNum = new BigDecimal(orderTransportResult.getTotalNum());
                    BigDecimal TootalNum = new BigDecimal(0);
                    TransportDispatchParam param = new TransportDispatchParam();
                    param.setTransportNumber(orderTransportResult.getTransportNumber());
                    List<TransportDispatchResult> list = transportDispatchService.findDispatchListBySpec(param, null);
                    for (TransportDispatchResult dispatchResult : list) {
                        BigDecimal num2 = new BigDecimal(dispatchResult.getTooDeployNum());
                        TootalNum = TootalNum.add(num2);
                    }
                    if (TotalNum.compareTo(TootalNum) == 0) {//判断商品是否  已经全部配置
                        //判断 配了几个车
                        boolean flag = basicPrivatecarService.findTwoByOrder(orderTransportParam, null);
                        if (!flag) {
                            orderTransportParam.setId(orderTransportResult.getId());
                            orderTransportParam.setTstate("已发运");
                            orderTransportService.update(orderTransportParam);
                        }
                    }
                }
            }

            BasicPrivatecar bpcr = basicPrivatecarService.getById(transportDispatchParam.getCarId());

            //计算满载率
            BigDecimal arr = new BigDecimal(100);
            BigDecimal privatecarSurevolume = new BigDecimal(bpcr.getPrivatecarSurevolume());//准载体积
            BigDecimal fulllv = toovolume1.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
            tDispatchParam.setFulllv(fulllv.toString());

            //更新运输单
            String lineName = "";
            if (line.size() == 1) {
                lineName = line.get(0);
            }
            if (line.size() > 1) {
                for (String str : line) {
                    lineName += str + ",";
                }
            }
            tDispatchParam.setLineName(lineName);
            if (transportNumber.length() < 250) {
                tDispatchParam.setTransportNumber(transportNumber);
            }
            if (customerOrder.length() < 250) {
                tDispatchParam.setCustomerOrder(customerOrder);
            }
            tDispatchParam.setTooNum(toonum1.toString());
            tDispatchParam.setToomoney(toomoney1.toString());
            tDispatchParam.setToovolume(toovolume1.toString());
            tDispatchParam.setId(tDispatchResult.getId());
            tDispatchService.update(tDispatchParam);
        }
        return ResponseData.success();
    }

    /**
     * 修改分配货物
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData editAssign(List<OrderTransportDetailedParam> _list, String carnumbger, ResponseData data) {
        if (_list.size() < 0) {
            data.setMessage("未勾选商品");
            data.setSuccess(false);
            return data;
        }
        User user = userService.getcurrentUser();
        Long deliverId = user.getDeliverId();
        Long deptId = user.getDeptId();
        for (OrderTransportDetailedParam tdp : _list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdp.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setGoodId(tdp.getGoodId());
            otdp.setTransportNumber(tdp.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细
            BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
            BigDecimal num = new BigDecimal(tdp.getNum());//分配数量
            BigDecimal volume = num.multiply(goodsVolume);//分配体积
            BigDecimal as = new BigDecimal(0);

            BasicPrivatecarParam bpp = new BasicPrivatecarParam();
            bpp.setPrivatecarCarnumber(carnumbger);
            bpp.setDeliverId(deliverId);
            BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp, deptId, user.getOperateId());//车辆

            //调度明细
            TransportDispatchParam tpdp = new TransportDispatchParam();
            tpdp.setTransportNumber(tdp.getTransportNumber());
            tpdp.setGoodsId(otdr.getGoodId());
            tpdp.setCarnumbger(carnumbger);
            TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);

            BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
            if (tooDeployNum.compareTo(num) == 0) {

            } else if (tooDeployNum.compareTo(num) == -1) {//增加
                //修改调度明细
                BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());//配置体积
                BigDecimal tooDeployNum2 = new BigDecimal(tdr2.getTooDeployNum());//配置数量
                BigDecimal volumes = volume.subtract(tooDeployVolume);//新增加体积
                BigDecimal nums = num.subtract(tooDeployNum2);//新增加数量
                tdr2.setTooDeployVolume(volume.toString());
                tdr2.setTooDeployNum(num.toString());
                ToolUtil.copyProperties(tdr2, tpdp);
                transportDispatchService.update(tpdp);

                //修改货物订单  已配置体积
                BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
                BigDecimal ntoovolume = toovolume.add(volumes);//已分配体积 + 新分配体积
                otr.setTooVolume(ntoovolume.toString());
                ToolUtil.copyProperties(otr, otp);
                orderTransportService.update(otp);

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.subtract(volumes);//未分配体积 - 新分配体积 = 新未分配体积
                BigDecimal newnotnum = notnum.subtract(nums);//未分配数量 - 新分配数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    data.setMessage("超出商品体积");
                    data.setSuccess(false);
                    return data;
                } else {
                    otdr.setNotnum(newnotnum.toString());
                    otdr.setNotvolume(newnotvolume.toString());
                    ToolUtil.copyProperties(otdr, otdp);
                    orderTransportDetailedService.update(otdp);
                }

                //修改车辆  已配置体积
                BigDecimal surevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
                BigDecimal toodeployvolume = new BigDecimal(bpr.getTooDeployvolume());//已配置体积
                BigDecimal ntoodeployvolume = toodeployvolume.add(volumes);//已配置体积 + 新配体积 = 新车辆已配体积
                if (ntoodeployvolume.compareTo(surevolume) == 1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    data.setMessage("超出车辆准载体积");
                    data.setSuccess(false);
                    return data;
                } else {
                    bpr.setTooDeployvolume(ntoodeployvolume.toString());
                    ToolUtil.copyProperties(bpr, bpp);
                    basicPrivatecarService.update(bpp);
                }
            } else if (tooDeployNum.compareTo(num) == 1) {//减少
                //修改调度明细
                BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());//配置体积
                BigDecimal tooDeployNum2 = new BigDecimal(tdr2.getTooDeployNum());//配置数量
                BigDecimal volumec = tooDeployVolume.subtract(volume);//减少体积
                BigDecimal numc = tooDeployNum2.subtract(num);//减少数量
                tdr2.setTooDeployVolume(volume.toString());
                tdr2.setTooDeployNum(num.toString());
                ToolUtil.copyProperties(tdr2, tpdp);
                transportDispatchService.update(tpdp);

                //修改货物订单  已配置体积
                BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
                BigDecimal ntoovolume = toovolume.subtract(volumec);//已分配体积 - 减少体积
                otr.setTooVolume(ntoovolume.toString());
                ToolUtil.copyProperties(otr, otp);
                orderTransportService.update(otp);

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.add(volumec);//未分配体积 + 减少体积 = 新未分配体积
                BigDecimal newnotnum = notnum.add(numc);//未分配数量 -  减少数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    data.setMessage("超出商品体积");
                    data.setSuccess(false);
                    return data;
                } else {
                    otdr.setNotnum(newnotnum.toString());
                    otdr.setNotvolume(newnotvolume.toString());
                    ToolUtil.copyProperties(otdr, otdp);
                    orderTransportDetailedService.update(otdp);
                }
                //修改车辆  已配置体积
                BigDecimal toodeployvolume = new BigDecimal(bpr.getTooDeployvolume());//已配置体积
                BigDecimal ntoodeployvolume = toodeployvolume.subtract(volumec);//已配置体积 - 新配体积 = 新车辆已配体积

                bpr.setTooDeployvolume(ntoodeployvolume.toString());
                ToolUtil.copyProperties(bpr, bpp);
                basicPrivatecarService.update(bpp);
            }
        }
        return ResponseData.success();
    }

    /**
     * 分配货物
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData assignNew(List<OrderTransportDetailedParam> _list, Long carId, String dispatchNumber, ResponseData data, User user, boolean calculateShippingCost) {
        if (_list.size() < 0) {
            data.setMessage("未勾选商品");
            data.setSuccess(false);
            return data;
        }
        BasicPrivatecar bpr = basicPrivatecarService.getById(carId);//车辆
        String carnumbger = bpr.getPrivatecarCarnumber();
        for (OrderTransportDetailedParam tdp : _list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdp.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单
            if (!"PENDING_DISPATCH".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("Order has changed");
            }
            if (calculateShippingCost) {
                if (otr.getLineId() == null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error(otr.getGetLocation() + "未设置所属线路!");
                }
            }

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setBatchnumber(tdp.getBatchnumber());
            otdp.setGoodId(tdp.getGoodId());
            otdp.setTransportNumber(tdp.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细
            BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
            BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
            BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//数量
            BigDecimal num = new BigDecimal(tdp.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配数量
            BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
            BigDecimal money = num.multiply(unitprice).setScale(2, BigDecimal.ROUND_HALF_DOWN);//分配金额
            if (ynum.compareTo(num) == 0) {
                volume = new BigDecimal(otdr.getTransportPutvolume());
                money = new BigDecimal(otdr.getTransportMoney());
            }
            BigDecimal as = new BigDecimal(0);
            if (num.compareTo(as) == 0) {
                continue;
            }

            //修改拣货单  已配置车牌号
            if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                if (otr.getOrderNumber() != null) {
                    StorageOutParam sop = new StorageOutParam();
                    sop.setStorageOutNumber(otr.getCustomerOrder());
                    sop.setOutOrderStatus("revoke");
                    try {
                        StorageOutResult sor = storageOutService.findBySpec(sop);
                        if (sor != null) {
                            StorageOutParam sopupdate = new StorageOutParam();
                            if (sor.getPrivatecarCarnumbers() != null) {
                                if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            } else {
                                sopupdate.setId(sor.getId());
                                sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                storageOutService.update(sopupdate);
                            }
                        }
                    } catch (Exception e) {
                    }
                }
            }

            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            BigDecimal ntoovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
            BigDecimal ntoonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量
            OrderTransportParam otpupdate = new OrderTransportParam();
            otpupdate.setId(otr.getId());
            otpupdate.setTooVolume(ntoovolume.toString());
            otpupdate.setTooNum(ntoonum.toString());
            orderTransportService.update(otpupdate);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
            BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                ResponseData.error("超出商品体积");
            } else {
                OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 输入类
                otdpupdate.setId(otdr.getId());
                otdpupdate.setNotnum(newnotnum.toString());
                otdpupdate.setNotvolume(newnotvolume.toString());
                orderTransportDetailedService.update(otdpupdate);
            }

            //新增调度明细
            TransportDispatchParam tpdp = new TransportDispatchParam();
            tpdp.setDispatchNumber(dispatchNumber);
            tpdp.setTransportNumber(tdp.getTransportNumber());
            tpdp.setCustomerOrder(otr.getCustomerOrder());
            tpdp.setCreateUserName(user.getName());
            tpdp.setGoodsId(otdr.getGoodId());
            if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                if (otr.getOrderSource().equals("零脉")) {
                    tpdp.setPositionId(otdr.getPositionId());
                }
            }
            tpdp.setDetailedDate(otdr.getDetailedDate());
            tpdp.setBatchnumber(otdr.getBatchnumber());
            tpdp.setCarId(carId);
            tpdp.setCarnumbger(carnumbger);
            tpdp.setCartel(bpr.getTel1());
            tpdp.setDriver1(bpr.getDriver1());
            TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
            //判断给此车 是否分配过此订单的 商品
            if (tdr2 != null) {
                BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                        if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                            if (otr.getDemolition().equals("拆零")) {
                                BigDecimal xianggui = new BigDecimal("10");
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        } else {
                            tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        }
                        if (otr.getOrderSource().equals("零脉")) {
                            BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                            BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                            BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                            tdr2.setJibenNum(tooAddjiben + "");
                        }
                    }
                    if (otr.getOrderSource().equals("WMS")) {
                        BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                        if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                            BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                            BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                            BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            tdr2.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        }
                    }
                }
                tdr2.setTransportMoney(transportMoney.add(money).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
                ToolUtil.copyProperties(tdr2, tpdp);
                transportDispatchService.update(tpdp);
            } else {
                tpdp.setTooDeployVolume(volume.toString());//分配的体积
                tpdp.setTooDeployNum(num.toString());//分配的数量
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                        if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                            if (otr.getDemolition().equals("拆零")) {
                                BigDecimal xianggui = new BigDecimal("10");
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());//分配的数量
                            }
                        } else {
                            tpdp.setMatrixingNumber(num.toString());//分配的数量
                        }
                        if (otr.getOrderSource().equals("零脉")) {
                            tpdp.setJibenUnit(otdr.getJibenUnit());
                            tpdp.setJibenNum(otdr.getJibenNum());
                            tpdp.setDetailedDate(otdr.getDetailedDate());
                            tpdp.setDaoqiDate(otdr.getDaoqiDate());
                            tpdp.setXlbPositionId(otdr.getXlbPositionId());
                            tpdp.setPositionId(otdr.getPositionId());
                            tpdp.setPositionNumber(otdr.getPositionNumber());
                            tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                            tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                            tpdp.setGoodsUnit(otdr.getGoodsUnit());
                            tpdp.setRate(otdr.getRate());
                        }
                    }
                    if (otr.getOrderSource().equals("WMS")) {
                        BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                        if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                            BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                            BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            tpdp.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            tpdp.setMatrixingNumber(num.toString());
                        }
                    }
                }
                tpdp.setTransportMoney(money.toString());//金额
                tpdp.setUnitprice(unitprice.toString());//单价
                tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                tpdp.setGoodsNumber(otdr.getGoodsNumber());
                tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                tpdp.setGoodsPacking(otdr.getDpacking());//包装
                tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                this.transportDispatchService.add(tpdp);
            }
        }
        return ResponseData.success();
    }

    @Resource
    private UserAop userAop;

    @Resource
    private TDispatchService tDispatchService;

    @Resource
    private FiFreightDetailsService fiFreightDetailsService;

    @Resource
    private TDispatchMapper tDispatchMapper;

    @Resource
    private FiFreightDetailsLogMapper fiFreightDetailsLogMapper;

    @Resource
    private TransportMsgService transportMsgService;

    @Resource
    private TransportCostService transportCostService;

    @Resource
    private TransportReceiptService transportReceiptService;

    /**
     * 撤销派车单
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData removeDispatchNumberBySpec(String dispatchNumber, ResponseData data, User user) {
        Long deptId = user.getDeptId();

        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(dispatchNumber);
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);
        if (tDispatchResult != null) {
            if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                if (ToolUtil.isNotEmpty(tDispatchResult.getXlbDepartOrderId())) {
                    return ResponseData.error("发车信息已上传新零帮");
                }
            }

            //已提报的不能再撤销
            if ("已提报".equals(tDispatchResult.getStateTb())) {
                return ResponseData.error("已提报状态，无法撤销");
            }

            if (tDispatchResult.getTstate().equals("待发运") || tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                TransportMsgParam msgParam = new TransportMsgParam();
                msgParam.setDispatchNumber(dispatchNumber);
                transportMsgService.deleteBySpec(msgParam);//删除运输信息
            }
            TransportCostParam costParam = new TransportCostParam();
            costParam.setDispatchNumber(dispatchNumber);
            transportCostService.deleteBySpec(costParam);//删除费用信息

            TransportReceiptParam receiptParam = new TransportReceiptParam();
            receiptParam.setDispatchNumber(dispatchNumber);
            transportReceiptService.deleteBySpec(receiptParam);//删除回传表信息

            TransportDispatchParam tdp = new TransportDispatchParam();
            tdp.setDispatchNumber(dispatchNumber);
            List<TransportDispatchResult> list = transportDispatchService.findListBySpec(tdp);
            for (TransportDispatchResult tdr : list) {
                OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
                otp.setTransportNumber(tdr.getTransportNumber());
                OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

                OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
                otdp.setGoodId(tdr.getGoodsId());
                otdp.setTransportNumber(tdr.getTransportNumber());
                otdp.setBatchnumber(tdr.getBatchnumber());
                otdp.setDetailedDate(tdr.getDetailedDate());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("零脉")) {
                        otdp.setPositionId(tdr.getPositionId());
                    }
                }
                OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

                BasicPrivatecar bpr = basicPrivatecarService.getById(tdr.getCarId());//车辆

                //调度明细
                BigDecimal tooDeployVolume = new BigDecimal(tdr.getTooDeployVolume());//配置体积
                BigDecimal tooDeployNum = new BigDecimal(tdr.getTooDeployNum());//配置数量

                //去除拣货单  已配置车牌号
                if (otr.getOrderNumber() != null) {
                    StorageOutParam sop = new StorageOutParam();
                    sop.setOutOrderNumber(otr.getOrderNumber());
                    sop.setOutOrderStatus("revoke");
                    StorageOutResult sor = storageOutService.findBySpec(sop);
                    if (sor != null) {
                        StorageOutParam sopupdate = new StorageOutParam();
                        if (sor.getPrivatecarCarnumbers() != null) {
                            sopupdate.setId(sor.getId());
                            sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(tDispatchResult.getCarnumber() + ";", ""));
                            storageOutService.update(sopupdate);
                        }
                    }
                }

                //修改货物订单  已配置体积
                BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
                BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
                BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
                BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
                OrderTransportParam otpupdate = new OrderTransportParam();
                otpupdate.setId(otr.getId());
                otpupdate.setTooVolume(ntoovolume.toString());
                otpupdate.setTooNum(ntoonum.toString());
                otpupdate.setTstate("待调");
                orderTransportService.update(otpupdate);

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
                BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
                BigDecimal as = new BigDecimal(0);
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("超出商品体积");
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }
                //修改车辆
                TDispatchParam tDispatchParamf = new TDispatchParam();
                tDispatchParamf.setCarId(tdr.getCarId());
                tDispatchParamf.setDeptId(deptId);
                tDispatchParamf.setDispatchNumber(tdr.getDispatchNumber());
                TDispatchResult tDispatchResultf = tDispatchService.findByYunshuCar(tDispatchParamf, user.getOperateId());
                if (tDispatchResultf == null) {//如果该车没有别的派车单
                    if (bpr != null) {
                        BasicPrivatecarParam bpp = new BasicPrivatecarParam();
                        bpp.setPrivatecarState("空闲");
                        bpp.setId(bpr.getId());
                        basicPrivatecarService.update(bpp);
                    }
                }
                //删除调度明细
                ToolUtil.copyProperties(tdr, tdp);
                transportDispatchService.delete(tdp);
            }
            //删除运输单
            tDispatchParam.setId(tDispatchResult.getId());
            tDispatchService.delete(tDispatchParam);
        }
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 180)
    public ResponseData jsfy(String ids) {
        if (ToolUtil.isEmpty(ids)) {
            return ResponseData.error("请选择计算费用数据");
        }
        List<String> list = ListUtil.of(ids.split(","));
        List<TDispatchResult> tDispatchResults = this.tDispatchMapper.findByIds(list);
        Map<String, List<FiFreightDetails>> fiFreightDetailsMap = null;
        FiFreightDetails fiFreightDetailsVo = null;
        SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf20 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BigDecimal big_0 = new BigDecimal(0);
        BigDecimal big_30 = new BigDecimal(30);
        BigDecimal big_60 = new BigDecimal(60);
        BigDecimal big_90 = new BigDecimal(90);
        BigDecimal big_120 = new BigDecimal(120);
        BigDecimal big_150 = new BigDecimal(150);
        BigDecimal big_180 = new BigDecimal(180);
        BigDecimal big_210 = new BigDecimal(210);
        BigDecimal big_250 = new BigDecimal(250);
        BigDecimal big_300 = new BigDecimal(300);
        BigDecimal big_400 = new BigDecimal(400);

        Integer[] henganPro = new Integer[]{51,52,54,56,57,58,60,61,62,63,64,65,66,67,68,69,70,71};
        try {
            List<String> disNumbers = new ArrayList<>();
            for (TDispatchResult obj : tDispatchResults) {
                disNumbers.add(obj.getDispatchNumber());
            }
            List<FiFreightDetails> list1 = this.fiFreightDetailsService.lambdaQuery().in(FiFreightDetails::getDispatchNumber, disNumbers).list();
            for(FiFreightDetails obj : list1){
                if(!"待审核".equals(obj.getProcessStatus()) && !"已申诉".equals(obj.getProcessStatus())){
                    return ResponseData.error(obj.getDispatchNumber() + "运输单非【待审核】和待审核状态，无法重新计算运费！");
                }
            }
            this.fiFreightDetailsService.delByDispatchNumber(disNumbers);
            this.fiFreightDetailsLogMapper.delByDispatchNumber(disNumbers);
            for (TDispatchResult obj : tDispatchResults) {
                fiFreightDetailsMap = new HashMap<>();
                TransportMsgParam q = new TransportMsgParam();
                q.setDispatchNumber(obj.getDispatchNumber());
                q.setStatus("到仓装车");
                TransportMsgResult dcqdByNumSpec = this.transportMsgMapper.findDcqdByNumSpec(q);
                //查询货运订单明细
                //(51,52,54,56,57,58,60,61,62,63,64,65,66,67,68,69,70,71)
                boolean exists = Arrays.asList(henganPro).contains(obj.getOperateId().intValue());
                TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
                transportDispatchParam.setDispatchNumber(obj.getDispatchNumber());
                transportDispatchParam.setCarId(obj.getCarId());
                transportDispatchParam.setDeptId(obj.getDeptId());
                if(exists){
                    transportDispatchParam.setHenganPro("恒安项目");
                }
                List<TransportDispatchResult> llist = transportDispatchService.findLineGetUnitListBySpecNew(transportDispatchParam, null, obj.getOperateId());//线路配置
                Map<String, CostLineSumVo> costLineSumMap = new HashMap<>();

                BigDecimal sumNum = BigDecimal.ZERO;
                BigDecimal sumVolume = BigDecimal.ZERO;
                //按照线路进行累加 体积或者件数
                for (TransportDispatchResult _obj : llist) {
                    fiFreightDetailsVo = new FiFreightDetails();
                    fiFreightDetailsVo.setOperateId(obj.getOperateId());//项目id
                    fiFreightDetailsVo.setOperateName(obj.getOperateName());//所属项目
                    fiFreightDetailsVo.setTrainNumber(obj.getTrainNumber());//车次
                    if (ToolUtil.isNotEmpty(obj.getStartDate())) {
                        try {
                            fiFreightDetailsVo.setDepartDate(sdf10.parse(obj.getStartDate()));//发车日期
                            if(ToolUtil.isNotEmpty(obj.getFayunTimes())){
                                if(obj.getFayunTimes().length() > 10){
                                    fiFreightDetailsVo.setDepartTime(sdf20.parse(obj.getFayunTimes()));//发运时间
                                }else{
                                    fiFreightDetailsVo.setDepartTime(sdf10.parse(obj.getFayunTimes()));//发运时间
                                }
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    if (dcqdByNumSpec != null && ToolUtil.isNotEmpty(dcqdByNumSpec.getTimes())) {
                        try {
                            fiFreightDetailsVo.setArrivalTime(sdf20.parse(dcqdByNumSpec.getTimes()));//到仓时间
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    fiFreightDetailsVo.setDispatchNumber(obj.getDispatchNumber());//调度单号
                    fiFreightDetailsVo.setCustomerOrder(_obj.getCustomerOrder());//客户单号
                    fiFreightDetailsVo.setPlateNumber(obj.getCarnumber());//车牌号
                    fiFreightDetailsVo.setSiji(obj.getSiji());//司机
                    fiFreightDetailsVo.setSijidianhua(obj.getSijidianhua());//司机电话
                    fiFreightDetailsVo.setChexing(obj.getChexing());//车型
                    fiFreightDetailsVo.setCity(_obj.getShi());//城市
                    fiFreightDetailsVo.setCounty(_obj.getQu());//区县
                    if (ToolUtil.isNotEmpty(obj.getMileageTraveled())) {
                        fiFreightDetailsVo.setMileageTraveled(new BigDecimal(obj.getMileageTraveled()));//行驶里程
                        BigDecimal mileageTraveled = fiFreightDetailsVo.getMileageTraveled();
                        if (mileageTraveled.compareTo(big_0) > -1 && mileageTraveled.compareTo(big_30) < 1) {
                            fiFreightDetailsVo.setMileageRange("0-30");
                        } else if (mileageTraveled.compareTo(big_30) > 0 && mileageTraveled.compareTo(big_60) < 1) {
                            fiFreightDetailsVo.setMileageRange("30-60");
                        } else if (mileageTraveled.compareTo(big_60) > 0 && mileageTraveled.compareTo(big_90) < 1) {
                            fiFreightDetailsVo.setMileageRange("60-90");
                        } else if (mileageTraveled.compareTo(big_90) > 0 && mileageTraveled.compareTo(big_120) < 1) {
                            fiFreightDetailsVo.setMileageRange("90-120");
                        } else if (mileageTraveled.compareTo(big_120) > 0 && mileageTraveled.compareTo(big_150) < 1) {
                            fiFreightDetailsVo.setMileageRange("120-150");
                        } else if (mileageTraveled.compareTo(big_150) > 0 && mileageTraveled.compareTo(big_180) < 1) {
                            fiFreightDetailsVo.setMileageRange("150-180");
                        } else if (mileageTraveled.compareTo(big_180) > 0 && mileageTraveled.compareTo(big_210) < 1) {
                            fiFreightDetailsVo.setMileageRange("180-210");
                        } else if (mileageTraveled.compareTo(big_210) > 0 && mileageTraveled.compareTo(big_250) < 1) {
                            fiFreightDetailsVo.setMileageRange("210-250");
                        } else if (mileageTraveled.compareTo(big_250) > 0 && mileageTraveled.compareTo(big_300) < 1) {
                            fiFreightDetailsVo.setMileageRange("250-300");
                        } else if (mileageTraveled.compareTo(big_300) > 0 && mileageTraveled.compareTo(big_400) < 1) {
                            fiFreightDetailsVo.setMileageRange("300-400");
                        } else if (mileageTraveled.compareTo(big_400) > 0) {
                            fiFreightDetailsVo.setMileageRange("400以上");
                        }
                    }
                    fiFreightDetailsVo.setLoadingType(obj.getLoadingType());//装车方
                    fiFreightDetailsVo.setLoadingNumber(obj.getLoadingNumber());//装车工号
                    fiFreightDetailsVo.setLoadingPersonnel(obj.getLoadingPersonnel());//装车人员
                    if (ToolUtil.isNotEmpty(obj.getZhunzailiang())) {
                        fiFreightDetailsVo.setZhunzailiang(new BigDecimal(obj.getZhunzailiang()));//准载量
                    }
                    fiFreightDetailsVo.setGetUnitsId(_obj.getGetUnitId());//收货客户
                    fiFreightDetailsVo.setGetUnits(_obj.getGetUnit());//收货客户
                    //city
                    //county
                    fiFreightDetailsVo.setToonum(_obj.getTooNum());//合计数量
                    if (ToolUtil.isNotEmpty(_obj.getTooDeployNum())) {//合计封箱数
                        fiFreightDetailsVo.setBoxNumber(new BigDecimal(_obj.getTooDeployNum()));
                    }else{
                        fiFreightDetailsVo.setBoxNumber(BigDecimal.ZERO);
                    }
                    if (ToolUtil.isNotEmpty(_obj.getTooDeployVolume())) {//合计体积
                        fiFreightDetailsVo.setToovolume(new BigDecimal(_obj.getTooDeployVolume()));
                    }else{
                        fiFreightDetailsVo.setToovolume(BigDecimal.ZERO);
                    }
                    fiFreightDetailsVo.setCarrierId(obj.getCarrierId());//承运商id
                    fiFreightDetailsVo.setCarrierName(obj.getCarrierName());//承运商
                    fiFreightDetailsVo.setLineId(_obj.getLineId());//线路ID
                    fiFreightDetailsVo.setLineName(_obj.getLineName());//线路
                    fiFreightDetailsVo.setDeptId(obj.getDeptId());//部门id
                    if (ToolUtil.isEmpty(_obj.getLineId())) {
//                        return ResponseData.error(_obj.getGetUnit() + "未设置所属线路");
                        throw new Exception(obj.getDispatchNumber()+"运输单中"+_obj.getGetUnit() + "未设置所属线路");
                    }
                    String lineStr = _obj.getLineId().toString();
                    //添加明细信息
                    if (fiFreightDetailsMap.get(lineStr) == null) {
                        List<FiFreightDetails> _fiFreightDetailsList = new ArrayList<>();
                        _fiFreightDetailsList.add(fiFreightDetailsVo);
                        fiFreightDetailsMap.put(lineStr, _fiFreightDetailsList);
                    } else {
                        fiFreightDetailsMap.get(lineStr).add(fiFreightDetailsVo);
                    }

                    if (costLineSumMap.get(lineStr) == null) {
                        costLineSumMap.put(lineStr, CostLineSumVo.builder()
                                .lineId(_obj.getLineId())
                                .lineName(_obj.getLineName())
                                .tooDeployNum(new BigDecimal(_obj.getTooDeployNum()).setScale(4, BigDecimal.ROUND_HALF_DOWN))
                                .tooDeployVolume(new BigDecimal(_obj.getTooDeployVolume()).setScale(4, BigDecimal.ROUND_HALF_DOWN))
                                .transportMoney(new BigDecimal(_obj.getTransportMoney()).setScale(4, BigDecimal.ROUND_HALF_DOWN))
                                .build());
                    } else {
                        costLineSumMap.put(lineStr, CostLineSumVo.builder()
                                .lineId(_obj.getLineId())
                                .lineName(_obj.getLineName())
                                .tooDeployNum(costLineSumMap.get(lineStr).getTooDeployNum().add(new BigDecimal(_obj.getTooDeployNum())))
                                .tooDeployVolume(costLineSumMap.get(lineStr).getTooDeployVolume().add(new BigDecimal(_obj.getTooDeployVolume())))
                                .transportMoney(costLineSumMap.get(lineStr).getTransportMoney().add(new BigDecimal(_obj.getTransportMoney())))
                                .build());
                    }
                    sumNum = sumNum.add(new BigDecimal(_obj.getTooDeployNum()).setScale(4, BigDecimal.ROUND_HALF_DOWN));
                    sumVolume = sumVolume.add(new BigDecimal(_obj.getTooDeployVolume()).setScale(4, BigDecimal.ROUND_HALF_DOWN));
                }
                //循环线路，获取单价，并计算运费
                List<TransportCost> costlist = new ArrayList<TransportCost>();
                BigDecimal totalConsultMoney = BigDecimal.ZERO; // 声明一个总价钱变量，初始值为0
                for (String _lineStr : costLineSumMap.keySet()) {
                    //根据线路 和承运商 获取对应的单价
                    BasicLinePriceParam basicLinePriceParam = new BasicLinePriceParam();
                    basicLinePriceParam.setCarrierId(obj.getCarrierId());
                    basicLinePriceParam.setLineId(costLineSumMap.get(_lineStr).getLineId());
                    List<BasicLinePriceResult> basicLinePricelist = basicLinePriceService.getlineprice(basicLinePriceParam);//查询线路报价的方法
                    if (basicLinePricelist == null || basicLinePricelist.size() < 1) {
                        basicLinePricelist = basicLinePriceService.getCarrierNulllineprice(basicLinePriceParam);//查询线路报价的方法
                    }
                    if (basicLinePricelist == null || basicLinePricelist.size() < 1) {
//                        return ResponseData.error(costLineSumMap.get(_lineStr).getLineName() + "线路未设置价格");
                        throw new Exception(obj.getDispatchNumber()+"运输单中"+costLineSumMap.get(_lineStr).getLineName() + "线路未设置价格");
                    }
                    //循环配置的计价模式 计算运费
                    for (BasicLinePriceResult __obj : basicLinePricelist) {
                        TransportCost addCost = new TransportCost();
                        addCost.setDispatchNumber(obj.getDispatchNumber());
                        addCost.setDeptId(obj.getDeptId());
                        addCost.setDeliverId(obj.getDeliverId());
                        addCost.setLineId(costLineSumMap.get(_lineStr).getLineId());
                        addCost.setLineName(costLineSumMap.get(_lineStr).getLineName());
                        addCost.setPriceName("运费");
                        addCost.setNums(costLineSumMap.get(_lineStr).getTooDeployVolume().toString());
                        addCost.setV2("0");
                        addCost.setMoney("0");

                        BigDecimal b2 = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getPrice())) {
                            b2 = new BigDecimal(__obj.getPrice());  //获取计价标准
                        }
                        BigDecimal volume = costLineSumMap.get(_lineStr).getTooDeployVolume();
                        BigDecimal num = costLineSumMap.get(_lineStr).getTooDeployNum();

                        //税率
                        BigDecimal taxRate = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getTaxRate())) {
                            taxRate = __obj.getTaxRate();  //税率
                        }
                        BigDecimal csxx = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getPriceCsxx())) {
                            csxx = new BigDecimal(__obj.getPriceCsxx()); //获取参数下限
                        }

                        BigDecimal cssx = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getPriceCssx())) {
                            cssx = new BigDecimal(__obj.getPriceCssx()); //获取参数上限
                        }

                        BigDecimal priceQb = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getPriceQb())) {
                            priceQb = new BigDecimal(__obj.getPriceQb()); //获取起步价
                        }

                        BigDecimal priceQbds = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getPriceQbds())) {
                            priceQbds = new BigDecimal(__obj.getPriceQbds()); //获取起步点数
                        }

                        BigDecimal prceQbcd = BigDecimal.ZERO;
                        if (ToolUtil.isNotEmpty(__obj.getPrceQbcd())) {
                            prceQbcd = new BigDecimal(__obj.getPrceQbcd()); //获取超点费用
                        }
                        //固定模式
                        if (ToolUtil.isEmpty(__obj.getPriceType()) || __obj.getPriceType().equals("固定")) {
                            if (__obj.getPriceMode().equals("元/m³")) {
                                BigDecimal b2mulvolume = volume.multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                totalConsultMoney = totalConsultMoney.add(b2mulvolume);

                                addCost.setPriceMode(__obj.getPriceMode());
                                addCost.setPrice(b2 + "");
                                addCost.setNums(volume + "");
                                addCost.setMoney(b2mulvolume + "");
                                for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                    __fiFreightDetails.setPrice(b2);
                                    __fiFreightDetails.setCalcCost(__fiFreightDetails.getToovolume().multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                                    __fiFreightDetails.setTaxRate(taxRate);
                                    __fiFreightDetails.setUntaxAmt(__fiFreightDetails.getCalcCost().divide(BigDecimal.ONE.add(taxRate),3, BigDecimal.ROUND_HALF_DOWN));
                                    __fiFreightDetails.setTaxAmt(__fiFreightDetails.getCalcCost().subtract(__fiFreightDetails.getUntaxAmt()));
                                }
                            } else if (__obj.getPriceMode().equals("元/件")) {
                                BigDecimal b2num = num.multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                totalConsultMoney = totalConsultMoney.add(b2num);
                                addCost.setPriceMode(__obj.getPriceMode());
                                addCost.setPrice(b2 + "");
                                addCost.setNums(num + "");
                                addCost.setMoney(b2num + "");
                                for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                    __fiFreightDetails.setPrice(b2);
                                    __fiFreightDetails.setCalcCost(__fiFreightDetails.getBoxNumber().multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                                    __fiFreightDetails.setTaxRate(taxRate);
                                    __fiFreightDetails.setUntaxAmt(__fiFreightDetails.getCalcCost().divide(BigDecimal.ONE.add(taxRate),3, BigDecimal.ROUND_HALF_DOWN));
                                    __fiFreightDetails.setTaxAmt(__fiFreightDetails.getCalcCost().subtract(__fiFreightDetails.getUntaxAmt()));
                                }
                            } else if (__obj.getPriceMode().equals("元/车")) {
                                totalConsultMoney = totalConsultMoney.add(b2);
                                addCost.setPriceMode(__obj.getPriceMode());
                                addCost.setPrice(b2 + "");
                                addCost.setNums("1");
                                addCost.setMoney(b2 + "");
                            } else if (__obj.getPriceMode().equals("元/点")) {
                                addCost.setPriceMode(__obj.getPriceMode());
                                addCost.setPrice(priceQb + "");
                                addCost.setV3(priceQbds + "");
                                addCost.setV4(prceQbcd + "");
                                BigDecimal money = priceQb;
                                totalConsultMoney = totalConsultMoney.add(priceQb);
                                // 计算是否超点
                                if (priceQbds.compareTo(BigDecimal.ZERO) == 1 && prceQbcd.compareTo(BigDecimal.ZERO) == 1) {
                                    TransportDispatchParam countppp = new TransportDispatchParam();
                                    countppp.setDispatchNumber(obj.getDispatchNumber());
                                    countppp.setLineId(costLineSumMap.get(_lineStr).getLineId());
                                    Integer countgg = transportDispatchService.countGetUnit(countppp);//合计该线路下  门店数
                                    if (countgg != null && countgg > 0) {
                                        addCost.setNums(countgg + "");
                                        BigDecimal bigcountgg = new BigDecimal(countgg);
                                        if (bigcountgg.compareTo(priceQbds) == 1) {//如果门店数 大于 起步点数
                                            BigDecimal countsubqbds = bigcountgg.subtract(priceQbds);
                                            BigDecimal cmcd = countsubqbds.multiply(prceQbcd).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                            money = money.add(cmcd);
                                            totalConsultMoney = totalConsultMoney.add(cmcd);
                                        }
                                    }
                                }
                                addCost.setMoney(money + "");
                            }
                        } else if (ToolUtil.isNotEmpty(__obj.getPriceType()) && __obj.getPriceType().equals("范围")) {
                            //参数范围
                            if (__obj.getPriceCsbz().equals("按件数")) {
                                BigDecimal fwajs = costLineSumMap.get(_lineStr).getTooDeployNum();
                                if (fwajs.compareTo(BigDecimal.ZERO) == 1) {
                                    if ((fwajs.compareTo(csxx) != -1) && (fwajs.compareTo(cssx) != 1)) { //大于等于参数下限 效应等于参数上限
                                        if (__obj.getPriceMode().equals("元/m³")) {
                                            BigDecimal b2mulvolume = volume.multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                            totalConsultMoney = totalConsultMoney.add(b2mulvolume);
                                            addCost.setPriceMode(__obj.getPriceMode());
                                            addCost.setPrice(b2 + "");
                                            addCost.setNums(volume + "");
                                            addCost.setMoney(b2mulvolume + "");
                                            for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                                __fiFreightDetails.setPrice(b2);
                                                __fiFreightDetails.setCalcCost(__fiFreightDetails.getToovolume().multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                                                __fiFreightDetails.setTaxRate(taxRate);
                                                __fiFreightDetails.setUntaxAmt(__fiFreightDetails.getCalcCost().divide(BigDecimal.ONE.add(taxRate),3, BigDecimal.ROUND_HALF_DOWN));
                                                __fiFreightDetails.setTaxAmt(__fiFreightDetails.getCalcCost().subtract(__fiFreightDetails.getUntaxAmt()));
                                            }
                                        } else if (__obj.getPriceMode().equals("元/件")) {
                                            BigDecimal b2num = num.multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                            totalConsultMoney = totalConsultMoney.add(b2num);
                                            addCost.setPriceMode(__obj.getPriceMode());
                                            addCost.setPrice(b2 + "");
                                            addCost.setNums(num + "");
                                            addCost.setMoney(b2num + "");
                                            for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                                __fiFreightDetails.setPrice(b2);
                                                __fiFreightDetails.setCalcCost(__fiFreightDetails.getBoxNumber().multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                                                __fiFreightDetails.setTaxRate(taxRate);
                                                __fiFreightDetails.setUntaxAmt(__fiFreightDetails.getCalcCost().divide(BigDecimal.ONE.add(taxRate),3, BigDecimal.ROUND_HALF_DOWN));
                                                __fiFreightDetails.setTaxAmt(__fiFreightDetails.getCalcCost().subtract(__fiFreightDetails.getUntaxAmt()));
                                            }
                                        } else if (__obj.getPriceMode().equals("元/车")) {
                                            totalConsultMoney = totalConsultMoney.add(b2);
                                            addCost.setPriceMode(__obj.getPriceMode());
                                            addCost.setPrice(b2 + "");
                                            addCost.setNums("1");
                                            addCost.setMoney(b2 + "");
                                        } else if (__obj.getPriceMode().equals("元/点")) {
                                            addCost.setPriceMode(__obj.getPriceMode());
                                            addCost.setPrice(priceQb + "");
                                            addCost.setV3(priceQbds + "");
                                            addCost.setV4(prceQbcd + "");
                                            BigDecimal money = priceQb;
                                            totalConsultMoney = totalConsultMoney.add(priceQb);
                                            // 计算是否超点
                                            if (priceQbds.compareTo(BigDecimal.ZERO) == 1 && prceQbcd.compareTo(BigDecimal.ZERO) == 1) {
                                                TransportDispatchParam countppp = new TransportDispatchParam();
                                                countppp.setDispatchNumber(obj.getDispatchNumber());
                                                countppp.setLineId(costLineSumMap.get(_lineStr).getLineId());
                                                Integer countgg = transportDispatchService.countGetUnit(countppp);//合计该线路下  门店数
                                                if (countgg != null && countgg > 0) {
                                                    addCost.setNums(countgg + "");
                                                    BigDecimal bigcountgg = new BigDecimal(countgg);
                                                    if (bigcountgg.compareTo(priceQbds) == 1) {//如果门店数 大于 起步点数
                                                        BigDecimal countsubqbds = bigcountgg.subtract(priceQbds);
                                                        BigDecimal cmcd = countsubqbds.multiply(prceQbcd).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                                        money = money.add(cmcd);
                                                        totalConsultMoney = totalConsultMoney.add(cmcd);
                                                    }
                                                }
                                            }
                                            addCost.setMoney(money + "");
                                        }
                                    }

                                }
                            } else if (__obj.getPriceCsbz().equals("按体积m³")) {
                                BigDecimal fwatj = costLineSumMap.get(_lineStr).getTooDeployVolume();
                                if ((fwatj.compareTo(csxx) != -1) && (fwatj.compareTo(cssx) != 1)) { //大于等于参数下限 小于等于参数上限
                                    if (__obj.getPriceMode().equals("元/m³")) {
                                        BigDecimal b2mulvolume = volume.multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                        totalConsultMoney = totalConsultMoney.add(b2mulvolume);
                                        addCost.setPriceMode(__obj.getPriceMode());
                                        addCost.setPrice(b2 + "");
                                        addCost.setNums(volume + "");
                                        addCost.setMoney(b2mulvolume + "");
                                        for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                            __fiFreightDetails.setPrice(b2);
                                            __fiFreightDetails.setCalcCost(__fiFreightDetails.getToovolume().multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                                            __fiFreightDetails.setTaxRate(taxRate);
                                            __fiFreightDetails.setUntaxAmt(__fiFreightDetails.getCalcCost().divide(BigDecimal.ONE.add(taxRate),3, BigDecimal.ROUND_HALF_DOWN));
                                            __fiFreightDetails.setTaxAmt(__fiFreightDetails.getCalcCost().subtract(__fiFreightDetails.getUntaxAmt()));
                                        }
                                    } else if (__obj.getPriceMode().equals("元/件")) {
                                        BigDecimal b2num = num.multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                        totalConsultMoney = totalConsultMoney.add(b2num);
                                        addCost.setPriceMode(__obj.getPriceMode());
                                        addCost.setPrice(b2 + "");
                                        addCost.setNums(num + "");
                                        addCost.setMoney(b2num + "");
                                        for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                            __fiFreightDetails.setPrice(b2);
                                            __fiFreightDetails.setCalcCost(__fiFreightDetails.getBoxNumber().multiply(b2).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                                            __fiFreightDetails.setTaxRate(taxRate);
                                            __fiFreightDetails.setUntaxAmt(__fiFreightDetails.getCalcCost().divide(BigDecimal.ONE.add(taxRate),3, BigDecimal.ROUND_HALF_DOWN));
                                            __fiFreightDetails.setTaxAmt(__fiFreightDetails.getCalcCost().subtract(__fiFreightDetails.getUntaxAmt()));
                                        }
                                    } else if (__obj.getPriceMode().equals("元/车")) {
                                        totalConsultMoney = totalConsultMoney.add(b2);
                                        addCost.setPriceMode(__obj.getPriceMode());
                                        addCost.setPrice(b2 + "");
                                        addCost.setNums("1");
                                        addCost.setMoney(b2 + "");
                                    } else if (__obj.getPriceMode().equals("元/点")) {
                                        addCost.setPriceMode(__obj.getPriceMode());
                                        addCost.setPrice(priceQb + "");
                                        addCost.setV3(priceQbds + "");
                                        addCost.setV4(prceQbcd + "");
                                        BigDecimal money = priceQb;
                                        totalConsultMoney = totalConsultMoney.add(priceQb);
                                        // 计算是否超点
                                        if (priceQbds.compareTo(BigDecimal.ZERO) == 1 && prceQbcd.compareTo(BigDecimal.ZERO) == 1) {
                                            TransportDispatchParam countppp = new TransportDispatchParam();
                                            countppp.setDispatchNumber(obj.getDispatchNumber());
                                            countppp.setLineId(costLineSumMap.get(_lineStr).getLineId());
                                            Integer countgg = transportDispatchService.countGetUnit(countppp);//合计该线路下  门店数
                                            if (countgg != null && countgg > 0) {
                                                addCost.setNums(countgg + "");
                                                BigDecimal bigcountgg = new BigDecimal(countgg);
                                                if (bigcountgg.compareTo(priceQbds) == 1) {//如果门店数 大于 起步点数
                                                    BigDecimal countsubqbds = bigcountgg.subtract(priceQbds);
                                                    BigDecimal cmcd = countsubqbds.multiply(prceQbcd).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                                    money = money.add(cmcd);
                                                    totalConsultMoney = totalConsultMoney.add(cmcd);
                                                }
                                            }
                                        }
                                        addCost.setMoney(money + "");
                                    }
                                }
                            }
                        }
                        costlist.add(addCost);
                    }
                }

                BigDecimal loadingMoney = new BigDecimal("0");  //创建2个计量对象赋值为0
                //装车费
                BigDecimal loadingPrice = null;
                BasicLoadingQuotationParam loadingQuotationParam = new BasicLoadingQuotationParam();  //创建装车表对象
                loadingQuotationParam.setOperateId(obj.getOperateId()); //填入传入调度单号项目id
                loadingQuotationParam.setCarrierId(obj.getCarrierId()); //承运商
//				loadingQuotationParam.setLineId(r.getLineId());//线路
                loadingQuotationParam.setLoadingType(obj.getLoadingType()); //装运方
                BasicLoadingQuotationResult basicLoadingQuotationResult = basicLoadingQuotationService.findBySpec(loadingQuotationParam);
                if (basicLoadingQuotationResult != null && ToolUtil.isNotEmpty(basicLoadingQuotationResult.getPrice())) {
                    if ("元/车".equals(basicLoadingQuotationResult.getPriceMode())) {
                        loadingPrice = new BigDecimal(basicLoadingQuotationResult.getPrice());
                        loadingMoney = loadingMoney.add(loadingPrice.setScale(3, BigDecimal.ROUND_HALF_DOWN));
                        //循环计算装车费
                        for (String _lineStr : fiFreightDetailsMap.keySet()) {
                            for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                __fiFreightDetails.setLoadingPrice(loadingPrice);
                                __fiFreightDetails.setLoadingMoney(loadingPrice);
                            }
                        }
                    } else if ("元/件".equals(basicLoadingQuotationResult.getPriceMode())) {
                        loadingPrice = new BigDecimal(basicLoadingQuotationResult.getPrice());
                        loadingMoney = loadingMoney.add(loadingPrice.multiply(sumNum).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                        //循环计算装车费
                        for (String _lineStr : fiFreightDetailsMap.keySet()) {
                            for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                __fiFreightDetails.setLoadingPrice(loadingPrice);
                                __fiFreightDetails.setLoadingMoney(loadingPrice.multiply(__fiFreightDetails.getBoxNumber()).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                            }
                        }
                    } else if ("元/m³".equals(basicLoadingQuotationResult.getPriceMode())) {
                        loadingPrice = new BigDecimal(basicLoadingQuotationResult.getPrice());
                        loadingMoney = loadingMoney.add(loadingPrice.multiply(sumVolume).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                        //循环计算装车费
                        for (String _lineStr : fiFreightDetailsMap.keySet()) {
                            for (FiFreightDetails __fiFreightDetails : fiFreightDetailsMap.get(_lineStr)) {
                                __fiFreightDetails.setLoadingPrice(loadingPrice);
                                __fiFreightDetails.setLoadingMoney(loadingPrice.multiply(__fiFreightDetails.getToovolume()).setScale(3, BigDecimal.ROUND_HALF_DOWN));
                            }
                        }
                    }
                }

                //批量保存
                for (String _lineStr : fiFreightDetailsMap.keySet()) {
                    this.fiFreightDetailsService.saveBatch(fiFreightDetailsMap.get(_lineStr));
                }
                if (obj.getCarrierName() != null && obj.getCarrierName().contains("临时车")) {
                    //修改运单信息
                    tDispatchService.lambdaUpdate()
                            .eq(TDispatch::getId, obj.getId())
                            .set(TDispatch::getConsultMoney, totalConsultMoney)
                            .set(TDispatch::getLoadingMoney, loadingMoney)
                            .set(TDispatch::getCalculateShippingCost, "是")
                            .update();
                } else {
                    //修改运单信息
                    tDispatchService.lambdaUpdate()
                            .eq(TDispatch::getId, obj.getId())
                            .set(TDispatch::getConsultMoney, totalConsultMoney)
                            .set(TDispatch::getFreightMoney, totalConsultMoney)
                            .set(TDispatch::getLoadingMoney, loadingMoney)
                            .set(TDispatch::getCalculateShippingCost, "是")
                            .update();
                }
                if (costlist.size() > 0) {
                    TransportCostParam delete = new TransportCostParam();
                    delete.setDispatchNumber(obj.getDispatchNumber());
                    transportCostService.deleteBySpec(delete);
                    transportCostService.saveBatch(costlist);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚
            return ResponseData.error(e.getMessage());
        }

        return ResponseData.success();
    }

    @Resource
    private BasicLinePriceService basicLinePriceService;

    @Resource
    private BasicLineService basicLineService;

    /**
     * 移除派车单订单
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 60)
    public ResponseData removeCustomerOrderDispatch(TransportDispatchParam transportDispatchParam, ResponseData data) {
        User user = userAop.getUser();
        Long deliverId = user.getDeliverId();
        Long deptId = user.getDeptId();
        TransportDispatchResult result = transportDispatchService.findBySpecNotEqCustomerOrder(transportDispatchParam);
        if (result == null) {//判断无其他已派商品明细  直接撤销派车单
            return this.removeDispatchNumberBySpec(transportDispatchParam.getDispatchNumber(), data, user);
        } else {
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setDispatchNumber(transportDispatchParam.getDispatchNumber());
            TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);//派车单
            BasicPrivatecar bpr = basicPrivatecarService.getById(result.getCarId());//车辆
            if (tDispatchResult != null) {
                if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                    return ResponseData.error("已发运无法删除");
                }
                List<TransportDispatchResult> list = transportDispatchService.findListBySpec(transportDispatchParam);
                BigDecimal as = new BigDecimal(0);
                OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
                otp.setCustomerOrder(transportDispatchParam.getCustomerOrder());
                OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单
                //修改货物订单  已配置体积 已配置数量
                BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
                BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
                for (TransportDispatchResult transportDispatch : list) {
                    TransportDispatchParam p = new TransportDispatchParam();
                    p.setId(transportDispatch.getId());
                    transportDispatchService.delete(p);//删除这条 派车明细

                    OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
                    otdp.setBatchnumber(transportDispatch.getBatchnumber());
                    otdp.setGoodId(transportDispatch.getGoodsId());
                    otdp.setTransportNumber(transportDispatch.getTransportNumber());
                    otdp.setDetailedDate(transportDispatch.getDetailedDate());
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("零脉")) {
                            otdp.setPositionId(transportDispatch.getPositionId());
                        }
                    }
                    OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

                    //调度明细
                    BigDecimal tooDeployVolume = new BigDecimal(transportDispatch.getTooDeployVolume());//配置体积
                    BigDecimal tooDeployNum = new BigDecimal(transportDispatch.getTooDeployNum());//配置数量

                    //去除拣货单  已配置车牌号
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("WMS")) {
                            if (otr.getOrderNumber() != null) {
                                TransportDispatchParam transportDispatchParam2 = new TransportDispatchParam();
                                transportDispatchParam2.setDispatchNumber(transportDispatch.getDispatchNumber());
                                transportDispatchParam2.setTransportNumber(transportDispatch.getTransportNumber());
                                TransportDispatchResult result2 = transportDispatchService.findBySpecNotEqId(transportDispatchParam2);
                                if (result2 == null) {
                                    StorageOutParam sop = new StorageOutParam();
                                    sop.setOutOrderNumber(otr.getOrderNumber());
                                    sop.setOutOrderStatus("revoke");
                                    StorageOutResult sor = storageOutService.findBySpec(sop);
                                    if (sor != null) {
                                        StorageOutParam sopupdate = new StorageOutParam();
                                        if (sor.getPrivatecarCarnumbers() != null) {
                                            sopupdate.setId(sor.getId());
                                            sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(tDispatchResult.getCarnumber() + ";", ""));
                                            storageOutService.update(sopupdate);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    toovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
                    toonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量

                    //修改订单明细  未分配体积,未分配数量
                    BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                    BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                    BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
                    BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
                    if (newnotnum.compareTo(as) == -1) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                        return ResponseData.error("超出商品体积");
                    } else {
                        otdp.setId(otdr.getId());
                        otdp.setNotnum(newnotnum.toString());
                        otdp.setNotvolume(newnotvolume.toString());
                        orderTransportDetailedService.update(otdp);
                    }
                }
                otp.setId(otr.getId());
                otp.setTooVolume(toovolume.toString());
                otp.setTooNum(toonum.toString());
                otp.setTstate("待调");
                orderTransportService.update(otp);

                if (list.size() > 0) {
                    TransportReceiptParam receiptParam = new TransportReceiptParam();
                    receiptParam.setDispatchNumber(tDispatchResult.getDispatchNumber());
                    transportReceiptService.deleteBySpec(receiptParam);//删除回传表信息

                    //添加回传单
                    OrderTransportParam oParam = new OrderTransportParam();
                    oParam.setDeliverId(deliverId);
                    oParam.setDeptId(deptId);
                    oParam.setOrderNumber(tDispatchResult.getDispatchNumber());
                    List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo(oParam, tDispatchResult.getCarId(), null, user.getOperateId());
                    String getUnits = "";
                    int deliverys = 1;
                    for (int i = 0; i < list2.size(); i++) {
                        OrderTransportResult orderTransportResult = list2.get(i);
                        TransportReceiptParam param = new TransportReceiptParam();
                        param.setDispatchNumber(tDispatchResult.getDispatchNumber());
                        param.setCarnumber(tDispatchResult.getCarnumber());
                        param.setCarId(tDispatchResult.getCarId());
                        param.setSetUnit(orderTransportResult.getSetUnit());
                        param.setSetLocation(orderTransportResult.getSetLocation());
                        param.setGetPeople(orderTransportResult.getGetPeople());
                        param.setGetTel(orderTransportResult.getGetTel());
                        param.setGetUnitid(orderTransportResult.getGetUnitId());
                        param.setGetUnit(orderTransportResult.getGetUnit());
                        getUnits += orderTransportResult.getGetUnit();
                        if ((i + 1) != list2.size()) {
                            getUnits += "+";
                        }
                        param.setGetLocation(orderTransportResult.getGetLocation());
                        param.setGetLocationid(orderTransportResult.getGetLocationId());
                        param.setSdtstate("未送达");
                        param.setTstate("未回传");
                        param.setNum(orderTransportResult.getTotalNum());
                        param.setVolume(orderTransportResult.getTooVolume());
                        param.setDeptId(deptId);
                        param.setDeliverId(deliverId);
                        param.setSetUnitId(orderTransportResult.getSetUnitId());
                        param.setSetLocationId(orderTransportResult.getSetLocationId());
                        param.setStockPort(orderTransportResult.getStockPort());
                        param.setDeliverys(deliverys + "");
                        param.setXlbClientId(orderTransportResult.getXlbClientId());
                        deliverys++;
                        transportReceiptService.add(param);
                    }
                    if (getUnits.length() < 300) {
                        tDispatchParam.setGetUnits(getUnits);
                    }

                    TransportDispatchParam p = new TransportDispatchParam();
                    p.setDispatchNumber(tDispatchResult.getDispatchNumber());
                    List<TransportDispatchResult> dlist = transportDispatchService.findListBySpec(p);
                    BigDecimal toonum1 = new BigDecimal(0);
                    BigDecimal toovolume1 = new BigDecimal(0);
                    BigDecimal toomoney1 = new BigDecimal(0);

                    String transportNumber = "";
                    String customerOrder = "";
                    List<String> line = new ArrayList<String>();
                    for (TransportDispatchResult dr : dlist) {
                        //合计数量体积
                        BigDecimal tooDeployNum1 = new BigDecimal(dr.getTooDeployNum());
                        BigDecimal tooDeployVolume1 = new BigDecimal(dr.getTooDeployVolume());
                        BigDecimal transportMoney1 = new BigDecimal(dr.getTransportMoney());
                        toonum1 = toonum1.add(tooDeployNum1).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                        toovolume1 = toovolume1.add(tooDeployVolume1).setScale(4, BigDecimal.ROUND_HALF_DOWN);
                        toomoney1 = toomoney1.add(transportMoney1).setScale(3, BigDecimal.ROUND_HALF_DOWN);

                        //查询货运订单
                        OrderTransportParam orderTransportParam = new OrderTransportParam();
                        orderTransportParam.setTransportNumber(dr.getTransportNumber());
                        OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

                        //新增运输费用
                        if (orderTransportResult.getLineId() != null) {
                            BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                            if (basicLine != null) {
                                if (basicLine.getStatus().equals("1")) {
                                    boolean yesflag = false;
                                    for (String str : line) {
                                        if (str.equals(basicLine.getLineName())) {
                                            yesflag = true;
                                            break;
                                        }
                                    }
                                    if (!yesflag) {
                                        line.add(basicLine.getLineName());
                                    }
                                }
                            }
                        }

                        if ("待调".equals(orderTransportResult.getTstate())) {
                            if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                                transportNumber += orderTransportResult.getTransportNumber() + ";";
                            }
                            if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                                customerOrder += orderTransportResult.getCustomerOrder() + ";";
                            }
                        }
                    }

                    //计算满载率
                    BigDecimal arr = new BigDecimal(100);
                    BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
                    BigDecimal fulllv = toovolume1.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
                    tDispatchParam.setFulllv(fulllv.toString());

                    //更新运输单
                    String lineName = "";
                    if (line.size() == 1) {
                        lineName = line.get(0);
                    }
                    if (line.size() > 1) {
                        for (String str : line) {
                            lineName += str + ",";
                        }
                    }
                    tDispatchParam.setLineName(lineName);
                    if (transportNumber.length() < 250) {
                        tDispatchParam.setTransportNumber(transportNumber);
                    }
                    if (customerOrder.length() < 250) {
                        tDispatchParam.setCustomerOrder(customerOrder);
                    }
                    tDispatchParam.setTooNum(toonum1.toString());
                    tDispatchParam.setToovolume(toovolume1.toString());
                    tDispatchParam.setToomoney(toomoney1.toString());
                    tDispatchParam.setId(tDispatchResult.getId());
                    tDispatchService.update(tDispatchParam);
                }
            }
        }
        return ResponseData.success();
    }

    /**
     * 移除派车单商品
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 60)
    public ResponseData removeGoodsDispatch(TransportDispatchParam transportDispatchParam, ResponseData data, User user) {
        Long deliverId = user.getDeliverId();
        Long deptId = user.getDeptId();
        TransportDispatch transportDispatch = transportDispatchService.getById(transportDispatchParam.getId());//派车明细
        transportDispatchParam.setDispatchNumber(transportDispatch.getDispatchNumber());
        TransportDispatchResult result = transportDispatchService.findBySpecNotEqId(transportDispatchParam);
        if (result == null) {//判断无其他已派商品明细  直接撤销派车单
            return this.removeDispatchNumberBySpec(transportDispatch.getDispatchNumber(), data, user);
        } else {
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setDispatchNumber(transportDispatch.getDispatchNumber());
            TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);//派车单
            if (tDispatchResult != null) {
                if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                    return ResponseData.error("已发运无法删除");
                }
                transportDispatchService.delete(transportDispatchParam);//删除这条 派车明细

                OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
                otp.setTransportNumber(transportDispatch.getTransportNumber());
                OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

                OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
                otdp.setGoodId(transportDispatch.getGoodsId());
                otdp.setBatchnumber(transportDispatch.getBatchnumber());
                otdp.setDetailedDate(transportDispatch.getDetailedDate());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("零脉")) {
                        otdp.setPositionId(transportDispatch.getPositionId());
                    }
                }
                otdp.setTransportNumber(transportDispatch.getTransportNumber());
                OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

                BasicPrivatecar bpr = basicPrivatecarService.getById(transportDispatch.getCarId());//车辆

                //调度明细
                BigDecimal tooDeployVolume = new BigDecimal(transportDispatch.getTooDeployVolume());//配置体积
                BigDecimal tooDeployNum = new BigDecimal(transportDispatch.getTooDeployNum());//配置数量

                //去除拣货单  已配置车牌号
                if (otr.getOrderNumber() != null) {
                    TransportDispatchParam transportDispatchParam2 = new TransportDispatchParam();
                    transportDispatchParam2.setDispatchNumber(transportDispatch.getDispatchNumber());
                    transportDispatchParam2.setTransportNumber(transportDispatch.getTransportNumber());
                    TransportDispatchResult result2 = transportDispatchService.findBySpecNotEqId(transportDispatchParam2);
                    if (result2 == null) {
                        StorageOutParam sop = new StorageOutParam();
                        sop.setOutOrderNumber(otr.getOrderNumber());
                        sop.setOutOrderStatus("revoke");
                        StorageOutResult sor = storageOutService.findBySpec(sop);
                        if (sor != null) {
                            StorageOutParam sopupdate = new StorageOutParam();
                            if (sor.getPrivatecarCarnumbers() != null) {
                                sopupdate.setId(sor.getId());
                                sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(tDispatchResult.getCarnumber() + ";", ""));
                                storageOutService.update(sopupdate);
                            }
                        }
                    }
                }

                //修改货物订单  已配置体积 已配置数量
                BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
                BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
                BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
                BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
                OrderTransportParam otpupdate = new OrderTransportParam();//货运订单 输入类
                otpupdate.setId(otr.getId());
                otpupdate.setTooVolume(ntoovolume.toString());
                otpupdate.setTooNum(ntoonum.toString());
                otpupdate.setTstate("待调");
                orderTransportService.update(otpupdate);

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
                BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
                BigDecimal as = new BigDecimal(0);
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("超出商品体积");
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 输入类
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }

                TransportReceiptParam receiptParam = new TransportReceiptParam();
                receiptParam.setDispatchNumber(transportDispatch.getDispatchNumber());
                transportReceiptService.deleteBySpec(receiptParam);//删除回传表信息

                //添加回传单
                OrderTransportParam oParam = new OrderTransportParam();
                oParam.setDeliverId(deliverId);
                oParam.setDeptId(deptId);
                oParam.setOrderNumber(transportDispatch.getDispatchNumber());
                List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo(oParam, transportDispatchParam.getCarId(), null, user.getOperateId());
                int deliverys = 1;
                String getUnits = "";
                for (int i = 0; i < list2.size(); i++) {
                    OrderTransportResult orderTransportResult = list2.get(i);
                    TransportReceiptParam param = new TransportReceiptParam();
                    param.setDispatchNumber(transportDispatch.getDispatchNumber());
                    param.setCarnumber(tDispatchResult.getCarnumber());
                    param.setCarId(tDispatchResult.getCarId());
                    param.setSetUnit(orderTransportResult.getSetUnit());
                    param.setSetLocation(orderTransportResult.getSetLocation());
                    param.setGetPeople(orderTransportResult.getGetPeople());
                    param.setGetTel(orderTransportResult.getGetTel());
                    param.setGetUnitid(orderTransportResult.getGetUnitId());
                    param.setGetUnit(orderTransportResult.getGetUnit());
                    getUnits += orderTransportResult.getGetUnit();
                    if ((i + 1) != list2.size()) {
                        getUnits += "+";
                    }
                    param.setGetLocation(orderTransportResult.getGetLocation());
                    param.setGetLocationid(orderTransportResult.getGetLocationId());
                    param.setSdtstate("未送达");
                    param.setTstate("未回传");
                    param.setNum(orderTransportResult.getTotalNum());
                    param.setVolume(orderTransportResult.getTooVolume());
                    param.setDeptId(deptId);
                    param.setDeliverId(deliverId);
                    param.setSetUnitId(orderTransportResult.getSetUnitId());
                    param.setSetLocationId(orderTransportResult.getSetLocationId());
                    param.setStockPort(orderTransportResult.getStockPort());
                    param.setDeliverys(deliverys + "");
                    param.setXlbClientId(orderTransportResult.getXlbClientId());
                    deliverys++;
                    transportReceiptService.add(param);
                }
                if (getUnits.length() < 300) {
                    tDispatchParam.setGetUnits(getUnits);
                }

                List<TransportDispatchResult> dlist = transportDispatchService.findListBySpec(transportDispatchParam);
                BigDecimal toonum1 = new BigDecimal(0);
                BigDecimal toovolume1 = new BigDecimal(0);
                BigDecimal toomoney1 = new BigDecimal(0);

                String transportNumber = "";
                String customerOrder = "";
                List<String> line = new ArrayList<String>();
                for (TransportDispatchResult dr : dlist) {
                    //合计数量体积
                    BigDecimal tooDeployNum1 = new BigDecimal(dr.getTooDeployNum());
                    BigDecimal tooDeployVolume1 = new BigDecimal(dr.getTooDeployVolume());
                    BigDecimal transportMoney1 = new BigDecimal(dr.getTransportMoney());
                    toonum1 = toonum1.add(tooDeployNum1).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                    toovolume1 = toovolume1.add(tooDeployVolume1).setScale(4, BigDecimal.ROUND_HALF_DOWN);
                    toomoney1 = toomoney1.add(transportMoney1).setScale(2, BigDecimal.ROUND_HALF_DOWN);

                    //查询货运订单
                    OrderTransportParam orderTransportParam = new OrderTransportParam();
                    orderTransportParam.setTransportNumber(dr.getTransportNumber());
                    OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

                    //新增运输费用
                    if (orderTransportResult.getLineId() != null) {
                        BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                        if (basicLine != null) {
                            if (basicLine.getStatus().equals("1")) {
                                boolean yesflag = false;
                                for (String str : line) {
                                    if (str.equals(basicLine.getLineName())) {
                                        yesflag = true;
                                        break;
                                    }
                                }
                                if (!yesflag) {
                                    line.add(basicLine.getLineName());
                                }
                            }
                        }
                    }

                    if ("待调".equals(orderTransportResult.getTstate())) {
                        if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                            transportNumber += orderTransportResult.getTransportNumber() + ";";
                        }
                        if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                            customerOrder += orderTransportResult.getCustomerOrder() + ";";
                        }
                    }
                }

                //计算满载率
                BigDecimal arr = new BigDecimal(100);
                BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
                BigDecimal fulllv = toovolume1.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
                tDispatchParam.setFulllv(fulllv.toString());

                //更新运输单
                String lineName = "";
                if (line.size() == 1) {
                    lineName = line.get(0);
                }
                if (line.size() > 1) {
                    for (String str : line) {
                        lineName += str + ",";
                    }
                }
                tDispatchParam.setLineName(lineName);
                if (transportNumber.length() < 250) {
                    tDispatchParam.setTransportNumber(transportNumber);
                }
                if (customerOrder.length() < 250) {
                    tDispatchParam.setCustomerOrder(customerOrder);
                }
                tDispatchParam.setTooNum(toonum1.toString());
                tDispatchParam.setToovolume(toovolume1.toString());
                tDispatchParam.setToomoney(toomoney1.toString());
                tDispatchParam.setId(tDispatchResult.getId());
                tDispatchService.update(tDispatchParam);
            }
        }
        return ResponseData.success();
    }

    /**
     * 移除车辆
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData removeCarBySpecNew(String getUnit, String getLocation, String carnumbger, ResponseData data) {
        User user = userAop.getUser();
        Long deptId = user.getDeptId();
        TransportDispatchParam tdp = new TransportDispatchParam();
        tdp.setCarnumbger(carnumbger);
        tdp.setDispatchNumber("yichu");
        tdp.setDeptId(deptId);
        List<TransportDispatchResult> list = transportDispatchService.findgoodslistBySpec(tdp, getUnit, getLocation, "Wait", null);
        for (TransportDispatchResult tdr : list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setGoodId(tdr.getGoodsId());
            otdp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

            BasicPrivatecarParam bpp = new BasicPrivatecarParam();
            bpp.setPrivatecarCarnumber(carnumbger);
            BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp, deptId, user.getOperateId());//车辆

            //调度明细
            BigDecimal tooDeployVolume = new BigDecimal(tdr.getTooDeployVolume());//配置体积
            BigDecimal tooDeployNum = new BigDecimal(tdr.getTooDeployNum());//配置数量

            //去除拣货单  已配置车牌号
            if (otr.getOrderNumber() != null) {
                StorageOutParam sop = new StorageOutParam();
                sop.setOutOrderNumber(otr.getOrderNumber());
                sop.setOutOrderStatus("revoke");
                StorageOutResult sor = storageOutService.findBySpec(sop);
                if (sor != null) {
                    if (sor.getPrivatecarCarnumbers() != null) {
                        sor.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(carnumbger + ";", ""));
                    }
                    ToolUtil.copyProperties(sor, sop);
                    storageOutService.update(sop);
                }
            }

            //修改货物订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
            BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntoonum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
            BigDecimal as = new BigDecimal(0);
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出商品体积");
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }
            //修改车辆  已配置体积
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
            tDispatchParam.setDeptId(deptId);
            TDispatchResult tDispatchResult = tDispatchService.findByYunshuCar(tDispatchParam, user.getOperateId());
            if (tDispatchResult == null) {
                bpr.setPrivatecarState("空闲");
                ToolUtil.copyProperties(bpr, bpp);
                basicPrivatecarService.update(bpp);
            }
            //删除调度明细
            ToolUtil.copyProperties(tdr, tdp);
            transportDispatchService.delete(tdp);
        }
        return ResponseData.success();
    }

    /**
     * web
     * 移除车辆
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData removeCarBySpecNewWeb(String carnumbger, ResponseData data, User user) {
        Long deptId = user.getDeptId();
        TransportDispatchParam tdp = new TransportDispatchParam();
        tdp.setCarnumbger(carnumbger);
        tdp.setDispatchNumber("yichu");
        tdp.setDeptId(deptId);
        List<TransportDispatchResult> list = transportDispatchService.findgoodslistBySpec(tdp, null, null, "Wait", null);
        for (TransportDispatchResult tdr : list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setGoodId(tdr.getGoodsId());
            otdp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

            BasicPrivatecarParam bpp = new BasicPrivatecarParam();
            bpp.setPrivatecarCarnumber(carnumbger);
            BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp, deptId, user.getOperateId());//车辆

            //调度明细
            BigDecimal tooDeployVolume = new BigDecimal(tdr.getTooDeployVolume());//配置体积
            BigDecimal tooDeployNum = new BigDecimal(tdr.getTooDeployNum());//配置数量

            //去除拣货单  已配置车牌号
            if (otr.getOrderNumber() != null) {
                StorageOutParam sop = new StorageOutParam();
                sop.setOutOrderNumber(otr.getOrderNumber());
                sop.setOutOrderStatus("revoke");
                StorageOutResult sor = storageOutService.findBySpec(sop);
                if (sor != null) {
                    if (sor.getPrivatecarCarnumbers() != null) {
                        sor.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(carnumbger + ";", ""));
                    }
                    ToolUtil.copyProperties(sor, sop);
                    storageOutService.update(sop);
                }
            }

            //修改货物订单  已配置体积
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
            BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntoonum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
            BigDecimal as = new BigDecimal(0);
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出商品体积");
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }
            //修改车辆  已配置体积
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
            tDispatchParam.setDeptId(deptId);
            TDispatchResult tDispatchResult = tDispatchService.findByYunshuCar(tDispatchParam, user.getOperateId());
            if (tDispatchResult == null) {
                bpr.setPrivatecarState("空闲");
                ToolUtil.copyProperties(bpr, bpp);
                basicPrivatecarService.update(bpp);
            }
            //删除调度明细
            ToolUtil.copyProperties(tdr, tdp);
            transportDispatchService.delete(tdp);
        }
        return ResponseData.success();
    }

    @Override
    public Integer findgoodslistNottransportNumber(TransportDispatchParam tdp, String token) {
        return this.baseMapper.findgoodslistNottransportNumber(tdp, token);
    }

    @Override
    public List<TransportDispatchResult> findCarListBySpecToo(TransportDispatchParam param,
                                                              String getUnit, String getLocation, String token, String carState, String dispatch, Long operateId) {
        return this.baseMapper.findCarListBySpecToo(param, getUnit, getLocation, token, carState, dispatch, operateId);
    }

    @Override
    public List<TransportDispatchResult> findCarListBySpecToo2(TransportDispatchParam param,
                                                               String getUnit, String getLocation, String token, String carState, String dispatch, Long operateId) {
        return this.baseMapper.findCarListBySpecToo2(param, getUnit, getLocation, token, carState, dispatch, operateId);
    }

    @Override
    public List<TransportDispatchResult> findCarListBySpecToo4(TransportDispatchParam param,
                                                               String getUnit, String getLocation, String token, String carState, String dispatch, Long operateId) {
        return this.baseMapper.findCarListBySpecToo4(param, getUnit, getLocation, token, carState, dispatch, operateId);
    }

    @Override
    public List<TransportDispatchResult> findCarListBySpecYunShuZhongToo(TransportDispatchParam param,
                                                                         String token, String carState, String dispatch, Long operateId) {
        return this.baseMapper.findCarListBySpecYunShuZhongToo(param, token, carState, dispatch, operateId);
    }

    @Override
    public LayuiPageInfo findCarListBySpecToo2Page(TransportDispatchParam param,
                                                   String getUnit, String getLocation, String token, String carState, String dispatch, Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findCarListBySpecToo2Page(pageContext, param, getUnit, getLocation, token, carState, dispatch, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findCarPageListBySpecToo(TransportDispatchParam param,
                                                  String getUnit, String getLocation, String token, String carState, String dispatch, Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findCarPageListBySpecToo(pageContext, param, getUnit, getLocation, token, carState, dispatch, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<TransportDispatchResult> findgoodslist(TransportDispatchParam orderTransportParam) {
        return this.baseMapper.findgoodslist(orderTransportParam);
    }

    @Override
    public List<TransportDispatchResult> findListBySpec(TransportDispatchParam param) {
        return this.baseMapper.findListBySpec(param);
    }

    /**
     * 追加拆单 明细
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData assignNewDispatchNumber(List<OrderTransportDetailedParam> _list, Long carId,
                                                ResponseData data, User user, String dispatchNumber, boolean calculateShippingCost) {
        if (_list.size() < 0) {
            data.setMessage("未勾选商品");
            data.setSuccess(false);
            return data;
        }

        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(dispatchNumber);
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);
        BasicPrivatecar bpr = basicPrivatecarService.getById(carId);//车辆
        String carnumbger = bpr.getPrivatecarCarnumber();
        for (OrderTransportDetailedParam tdp : _list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdp.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单
            if (!otr.getOperateId().equals(tDispatchResult.getOperateId())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("Documents belong to different projects");
            }
            if (!"待调".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单发生变动");
            }
            if (calculateShippingCost) {
                if (otr.getLineId() == null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error(otr.getGetUnit() + "Route not set!");
                }
            }
            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setBatchnumber(tdp.getBatchnumber());
            otdp.setGoodId(tdp.getGoodId());
            otdp.setTransportNumber(tdp.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细
            BigDecimal as = new BigDecimal(0);
            BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
            BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
            BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//数量
            BigDecimal num = new BigDecimal(tdp.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配数量
            BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
            BigDecimal money = num.multiply(unitprice).setScale(2, BigDecimal.ROUND_HALF_DOWN);//分配金额
            if (ynum.compareTo(num) == 0) {
                volume = new BigDecimal(otdr.getTransportPutvolume());
                money = new BigDecimal(otdr.getTransportMoney());
            }
            if (num.compareTo(as) == 0) {
                continue;
            }

            //修改拣货单  已配置车牌号
            if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                if (otr.getOrderNumber() != null) {
                    StorageOutParam sop = new StorageOutParam();
                    sop.setStorageOutNumber(otr.getCustomerOrder());
                    sop.setOutOrderStatus("revoke");
                    try {
                        StorageOutResult sor = storageOutService.findBySpec(sop);
                        if (sor != null) {
                            StorageOutParam sopupdate = new StorageOutParam();
                            if (sor.getPrivatecarCarnumbers() != null) {
                                if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            } else {
                                sopupdate.setId(sor.getId());
                                sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                storageOutService.update(sopupdate);
                            }
                        }
                    } catch (Exception e) {
                    }
                }
            }

            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            BigDecimal ntoovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
            BigDecimal ntoonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量
            BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
            if (ntoonum.compareTo(totalNum) == 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("超出订单商品体积");
            }
            if (totalNum.compareTo(ntoonum) == 0) {
                boolean flag = basicPrivatecarService.findTwoByOrder(otp, null);//判断 配了几个车
                if (!flag) {
                    otp.setTstate("已发运");
                }
            }
            otp.setId(otr.getId());
            otp.setTooVolume(ntoovolume.toString());
            otp.setTooNum(ntoonum.toString());
            orderTransportService.update(otp);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
            BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                ResponseData.error("超出商品体积");
            } else {
                OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();
                otdpupdate.setId(otdr.getId());
                otdpupdate.setNotnum(newnotnum.toString());
                otdpupdate.setNotvolume(newnotvolume.toString());
                orderTransportDetailedService.update(otdpupdate);
            }

            //新增调度明细
            TransportDispatchParam tpdp = new TransportDispatchParam();
            tpdp.setDispatchNumber(dispatchNumber);
            tpdp.setTransportNumber(tdp.getTransportNumber());
            tpdp.setCustomerOrder(otr.getCustomerOrder());
            tpdp.setCreateUserName(user.getName());
            tpdp.setGoodsId(otdr.getGoodId());
            if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                if (otr.getOrderSource().equals("零脉")) {
                    tpdp.setPositionId(otdr.getPositionId());
                }
            }
            tpdp.setDetailedDate(otdr.getDetailedDate());
            tpdp.setBatchnumber(otdr.getBatchnumber());
            tpdp.setCarnumbger(carnumbger);
            tpdp.setCarId(carId);
            tpdp.setCartel(bpr.getTel1());
            tpdp.setDriver1(bpr.getDriver1());
            TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
            //判断给此车 是否分配过此订单的 商品
            if (tdr2 != null) {
                BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                        if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                            if (otr.getDemolition().equals("拆零")) {
                                BigDecimal xianggui = new BigDecimal("10");
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        } else {
                            tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        }
                        if (otr.getOrderSource().equals("零脉")) {
                            BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                            BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                            BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                            tdr2.setJibenNum(tooAddjiben + "");
                        }
                    }
                    if (otr.getOrderSource().equals("WMS")) {
                        BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                        if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                            BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                            BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                            BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            tdr2.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                        }
                    }
                }
                tdr2.setTransportMoney(transportMoney.add(money).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                ToolUtil.copyProperties(tdr2, tpdp);
                transportDispatchService.update(tpdp);
            } else {
                tpdp.setTooDeployVolume(volume.toString());//分配的体积
                tpdp.setTooDeployNum(num.toString());//分配的数量
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                        if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                            if (otr.getDemolition().equals("拆零")) {
                                BigDecimal xianggui = new BigDecimal("10");
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                        } else {
                            tpdp.setMatrixingNumber(num.toString());
                        }
                        if (otr.getOrderSource().equals("零脉")) {
                            tpdp.setJibenUnit(otdr.getJibenUnit());
                            tpdp.setJibenNum(otdr.getJibenNum());
                            tpdp.setDetailedDate(otdr.getDetailedDate());
                            tpdp.setDaoqiDate(otdr.getDaoqiDate());
                            tpdp.setXlbPositionId(otdr.getXlbPositionId());
                            tpdp.setPositionId(otdr.getPositionId());
                            tpdp.setPositionNumber(otdr.getPositionNumber());
                            tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                            tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                            tpdp.setGoodsUnit(otdr.getGoodsUnit());
                            tpdp.setRate(otdr.getRate());
                        }
                    }
                    if (otr.getOrderSource().equals("WMS")) {
                        BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                        if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                            BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                            BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                            tpdp.setMatrixingNumber(matrixingNumber.toString());
                        } else {
                            tpdp.setMatrixingNumber(num.toString());
                        }
                    }
                }
                tpdp.setTransportMoney(money.toString());//金额
                tpdp.setUnitprice(unitprice.toString());//单价
                tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                tpdp.setGoodsNumber(otdr.getGoodsNumber());
                tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                tpdp.setGoodsPacking(otdr.getDpacking());//包装
                tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                this.transportDispatchService.add(tpdp);
            }
        }
        return ResponseData.success();
    }

    /**
     * 追加明细
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData batchassignNewDispatchNumber_driver(List<OrderTransportDetailedParam> _list, String dispatchNumber,
                                                            ResponseData data, User user, BasicOperate basicOperate, String scanDress) {
        if (_list.size() == 0) {
            return ResponseData.error("未获取到选择明细");
        }
        boolean calculateShippingCost = false;
        if (basicOperate != null && ToolUtil.isNotEmpty(basicOperate.getCalculateShippingCost())) {
            if (basicOperate.getCalculateShippingCost().equals("是")) {
                calculateShippingCost = true;
            }
        }
        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(dispatchNumber);
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);
        BasicPrivatecar bpr = basicPrivatecarService.getById(tDispatchResult.getCarId());//车辆
        if (tDispatchResult != null) {
            if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("已发运无法追加");
            }
        }
        String carnumbger = tDispatchResult.getCarnumber();
        BigDecimal as = new BigDecimal(0);
        for (OrderTransportDetailedParam otr2 : _list) {
            if (otr2.getGoodId() != null && otr2.getGoodId() != 0L) {
                List<OrderTransportDetailedParam> _list2 = new ArrayList<OrderTransportDetailedParam>();
                _list2.add(otr2);
                try {
                    data = transportDispatchService.assignNewDispatchNumber(_list2, tDispatchResult.getCarId(), data, user, dispatchNumber, calculateShippingCost);
                    if (!data.getSuccess()) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                        return data;
                    }
                } catch (Exception e) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("后台异常");
                }
                continue;
            }
            OrderTransportResult otr = new OrderTransportResult();
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(otr2.getTransportNumber());
            if (ToolUtil.isNotEmpty(scanDress) && scanDress.equals("是")) {
                otr = orderTransportService.findByTransportNumberOrOrderNumber(otp);//货运订单
            } else {
                otr = orderTransportService.findBySpec(otp, null);
            }
            if (otr == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单不存在！");
            }
            otp.setTransportNumber(otr.getTransportNumber());
            Integer count = orderTransportService.lambdaQuery()
                    .eq(OrderTransport::getCustomerOrder, otr.getCustomerOrder())
                    .eq(OrderTransport::getOperateId,tDispatchResult.getOperateId())
                    .count();
            if (count > 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据数据重复异常！请联系管理员");
            }
            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            if (!otr.getOperateId().equals(tDispatchResult.getOperateId())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据所属项目不一致");
            }
            if (!"待调".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单发生变动或已配置");
            }
            if (calculateShippingCost) {
                if (otr.getLineId() == null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error(otr.getGetUnit() + "未设置所属线路!");
                }
            }
            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setTransportNumber(otr.getTransportNumber());
            List<OrderTransportDetailedResult> list = orderTransportDetailedService.findListBySpec(otdp);//订单明细
            for (OrderTransportDetailedResult otdr : list) {
                BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
                BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//数量
                BigDecimal num = new BigDecimal(otdr.getNotnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配数量
                BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
                BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
                BigDecimal money = num.multiply(unitprice).setScale(2, BigDecimal.ROUND_HALF_DOWN);//分配金额
                if (ynum.compareTo(num) == 0) {
                    volume = new BigDecimal(otdr.getTransportPutvolume());
                    money = new BigDecimal(otdr.getTransportMoney());
                }
                if (num.compareTo(as) == 0) {
                    continue;
                }

                //修改拣货单  已配置车牌号
                if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                    if (otr.getOrderNumber() != null) {
                        StorageOutParam sop = new StorageOutParam();
                        sop.setStorageOutNumber(otr.getCustomerOrder());
                        sop.setOutOrderStatus("revoke");
                        try {
                            StorageOutResult sor = storageOutService.findBySpec(sop);
                            if (sor != null) {
                                StorageOutParam sopupdate = new StorageOutParam();
                                if (sor.getPrivatecarCarnumbers() != null) {
                                    if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                        sopupdate.setId(sor.getId());
                                        sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                        storageOutService.update(sopupdate);
                                    }
                                } else {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                toovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
                toonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量
                BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
                if (toonum.compareTo(totalNum) == 1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("超出订单商品体积");
                }
                if (totalNum.compareTo(toonum) == 0) {
                    boolean flag = basicPrivatecarService.findTwoByOrder(otp, null);//判断 配了几个车
                    if (!flag) {
                        otp.setTstate("已发运");
                    }
                }

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
                BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("超出商品体积");
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 输入类
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }

                //新增调度明细
                TransportDispatchParam tpdp = new TransportDispatchParam();
                tpdp.setDispatchNumber(dispatchNumber);
                tpdp.setTransportNumber(otr.getTransportNumber());
                tpdp.setCustomerOrder(otr.getCustomerOrder());
                tpdp.setCreateUserName(user.getName());
                tpdp.setBatchnumber(otdr.getBatchnumber());
                tpdp.setDetailedDate(otdr.getDetailedDate());
                tpdp.setGoodsId(otdr.getGoodId());
                tpdp.setCarId(tDispatchResult.getCarId());
                tpdp.setCarnumbger(carnumbger);
                tpdp.setCartel(bpr.getTel1());
                tpdp.setDriver1(bpr.getDriver1());
                TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
                //判断给此车 是否分配过此订单的 商品
                if (tdr2 != null) {
                    BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                    BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                    BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                    tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                    tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                    BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tdr2.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                                }
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                                BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                                BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setJibenNum(tooAddjiben + "");
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        }
                    }
                    tdr2.setTransportMoney(transportMoney.add(money).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(tdr2, tpdp);
                    transportDispatchService.update(tpdp);
                } else {
                    tpdp.setTooDeployVolume(volume.toString());//分配的体积
                    tpdp.setTooDeployNum(num.toString());//分配的数量
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tpdp.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tpdp.setMatrixingNumber(num.toString());
                                }
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                tpdp.setJibenUnit(otdr.getJibenUnit());
                                tpdp.setJibenNum(otdr.getJibenNum());
                                tpdp.setDetailedDate(otdr.getDetailedDate());
                                tpdp.setDaoqiDate(otdr.getDaoqiDate());
                                tpdp.setXlbPositionId(otdr.getXlbPositionId());
                                tpdp.setPositionId(otdr.getPositionId());
                                tpdp.setPositionNumber(otdr.getPositionNumber());
                                tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                                tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                                tpdp.setGoodsUnit(otdr.getGoodsUnit());
                                tpdp.setRate(otdr.getRate());
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                        }
                    }
                    tpdp.setTransportMoney(money.toString());//金额
                    tpdp.setUnitprice(unitprice.toString());//单价
                    tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                    tpdp.setGoodsNumber(otdr.getGoodsNumber());
                    tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                    tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                    tpdp.setGoodsPacking(otdr.getDpacking());//包装
                    tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                    this.transportDispatchService.add(tpdp);
                }
            }
            otp.setId(otr.getId());
            otp.setTooVolume(toovolume.toString());
            otp.setTooNum(toonum.toString());
            orderTransportService.update(otp);
        }

        //重新计算派车单信息
        TransportReceiptParam receiptParam = new TransportReceiptParam();
        receiptParam.setDispatchNumber(tDispatchResult.getDispatchNumber());
        transportReceiptService.deleteBySpec(receiptParam);//删除回传表信息

        //添加回传单
        OrderTransportParam oParam = new OrderTransportParam();
        oParam.setDeptId(basicOperate.getDeptId());
        oParam.setOrderNumber(tDispatchResult.getDispatchNumber());
        List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo(oParam, tDispatchResult.getCarId(), null, Long.parseLong(basicOperate.getId().toString()));
        int deliverys = 1;
        String getUnits = "";
        for (int i = 0; i < list2.size(); i++) {
            OrderTransportResult orderTransportResult = list2.get(i);
            TransportReceiptParam param = new TransportReceiptParam();
            param.setDispatchNumber(tDispatchResult.getDispatchNumber());
            param.setCarId(tDispatchResult.getCarId());
            param.setCarnumber(tDispatchResult.getCarnumber());
            param.setSetUnit(orderTransportResult.getSetUnit());
            param.setSetLocation(orderTransportResult.getSetLocation());
            param.setGetPeople(orderTransportResult.getGetPeople());
            param.setGetTel(orderTransportResult.getGetTel());
            param.setGetUnitid(orderTransportResult.getGetUnitId());
            param.setGetUnit(orderTransportResult.getGetUnit());
            getUnits += orderTransportResult.getGetUnit();
            if ((i + 1) != list2.size()) {
                getUnits += "+";
            }
            param.setGetLocation(orderTransportResult.getGetLocation());
            param.setGetLocationid(orderTransportResult.getGetLocationId());
            param.setSdtstate("未送达");
            param.setTstate("未回传");
            param.setNum(orderTransportResult.getTotalNum());
            param.setVolume(orderTransportResult.getTooVolume());
            param.setDeptId(basicOperate.getDeptId());
            param.setDeliverId(basicOperate.getDeliverId());
            param.setSetUnitId(orderTransportResult.getSetUnitId());
            param.setSetLocationId(orderTransportResult.getSetLocationId());
            param.setStockPort(orderTransportResult.getStockPort());
            param.setDeliverys(deliverys + "");
            param.setXlbClientId(orderTransportResult.getXlbClientId());
            deliverys++;
            transportReceiptService.add(param);
        }
        if (list2.size() == 1) {
            tDispatchResult.setMileageTraveled(list2.get(0).getKilometer());
        } else {
            tDispatchResult.setMileageTraveled("0");
        }
        tDispatchResult.setGetUnits(getUnits);

        TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
        transportDispatchParam.setDispatchNumber(tDispatchResult.getDispatchNumber());
        transportDispatchParam.setCarId(tDispatchResult.getCarId());
        transportDispatchParam.setDeptId(basicOperate.getDeptId());
        List<TransportDispatchResult> dlist = transportDispatchService.findOrderListBySpec(transportDispatchParam, null, Long.parseLong(basicOperate.getId().toString()));
        BigDecimal toonum1 = new BigDecimal(0);
        BigDecimal toovolume1 = new BigDecimal(0);
        BigDecimal toomoney1 = new BigDecimal(0);
        BigDecimal consultMoney = new BigDecimal(0);

        String transportNumber = "";
        String customerOrder = "";
        List<String> line = new ArrayList<String>();
        for (TransportDispatchResult dr : dlist) {
            //合计数量体积
            BigDecimal tooDeployNum1 = new BigDecimal(dr.getTooDeployNum());
            BigDecimal transportMoney1 = new BigDecimal(dr.getTransportMoney());
            BigDecimal tooDeployVolume1 = new BigDecimal(dr.getTooDeployVolume());
            toonum1 = toonum1.add(tooDeployNum1).setScale(3, BigDecimal.ROUND_HALF_DOWN);
            toomoney1 = toomoney1.add(transportMoney1).setScale(2, BigDecimal.ROUND_HALF_DOWN);
            toovolume1 = toovolume1.add(tooDeployVolume1).setScale(4, BigDecimal.ROUND_HALF_DOWN);

            //查询货运订单
            OrderTransportParam orderTransportParam = new OrderTransportParam();
            orderTransportParam.setTransportNumber(dr.getTransportNumber());
            OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

            //新增运输费用
            if (orderTransportResult.getLineId() != null) {
                BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                if (basicLine != null) {
                    if (basicLine.getStatus().equals("1")) {
                        boolean yesflag = false;
                        for (String str : line) {
                            if (str.equals(basicLine.getLineName())) {
                                yesflag = true;
                                break;
                            }
                        }
                        if (!yesflag) {
                            line.add(basicLine.getLineName());
                        }
                    }
                }
            }

            if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                transportNumber += orderTransportResult.getTransportNumber() + ";";
            }
            if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                customerOrder += orderTransportResult.getCustomerOrder() + ";";
            }
        }

        //计算满载率
        BigDecimal arr = new BigDecimal(100);
        BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
        BigDecimal fulllv = toovolume1.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
        tDispatchResult.setFulllv(fulllv.toString());

        //更新运输单
        String lineName = "";
        if (line.size() == 1) {
            lineName = line.get(0);
        }
        if (line.size() > 1) {
            for (String str : line) {
                lineName += str + ",";
            }
        }
        tDispatchResult.setLineName(lineName);
        if (transportNumber.length() < 250) {
            tDispatchResult.setTransportNumber(transportNumber);
        }
        if (customerOrder.length() < 250) {
            tDispatchResult.setCustomerOrder(customerOrder);
        }
        tDispatchResult.setTooNum(toonum1.toString());
        tDispatchResult.setToovolume(toovolume1.toString());
        tDispatchResult.setToomoney(toomoney1.toString());
        tDispatchResult.setConsultMoney(consultMoney.toString());
        ToolUtil.copyProperties(tDispatchResult, tDispatchParam);
        tDispatchService.update(tDispatchParam);
        return ResponseData.success();
    }

    /**
     * 追加明细
     */
    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData batchassignNewDispatchNumber(List<OrderTransportDetailedParam> _list, String dispatchNumber,
                                                     ResponseData data, User user) {
        if (_list.size() == 0) {
            return ResponseData.error("未获取到选择明细");
        }
        Long deptId = user.getDeptId();
        boolean calculateShippingCost = false;
        if (user.getOperateId() != null) {
            BasicOperate basicOperate = basicOperateService.getById(user.getOperateId());
            if (basicOperate != null && ToolUtil.isNotEmpty(basicOperate.getCalculateShippingCost())) {
                if (basicOperate.getCalculateShippingCost().equals("是")) {
                    calculateShippingCost = true;
                }
            }
        }
        TDispatchParam tDispatchParam = new TDispatchParam();
        tDispatchParam.setDispatchNumber(dispatchNumber);
        TDispatchResult tDispatchResult = tDispatchService.findBySpec(tDispatchParam, null);
        BasicPrivatecar bpr = basicPrivatecarService.getById(tDispatchResult.getCarId());//车辆
        if (tDispatchResult != null) {
            if (tDispatchResult.getTstate().equals("运输中") || tDispatchResult.getTstate().equals("已完成")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("已发运无法追加");
            }
        }
        String carnumbger = tDispatchResult.getCarnumber();
        BigDecimal as = new BigDecimal(0);
        for (OrderTransportDetailedParam otr2 : _list) {
            if (otr2.getGoodId() != null && otr2.getGoodId() != 0L) {
                List<OrderTransportDetailedParam> _list2 = new ArrayList<OrderTransportDetailedParam>();
                _list2.add(otr2);
                try {
                    data = transportDispatchService.assignNewDispatchNumber(_list2, tDispatchResult.getCarId(), data, user, dispatchNumber, calculateShippingCost);
                    if (!data.getSuccess()) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                        return data;
                    }
                } catch (Exception e) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("后台异常");
                }
                continue;
            }

            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(otr2.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单
            Integer count = orderTransportService.lambdaQuery()
                    .eq(OrderTransport::getCustomerOrder, otr.getCustomerOrder())
                    .eq(OrderTransport::getOperateId,tDispatchResult.getOperateId())
                    .count();
            if (count > 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据数据重复异常！请联系管理员");
            }
            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            if (!otr.getOperateId().equals(tDispatchResult.getOperateId())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据所属项目不一致");
            }
            if (!"待调".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单发生变动");
            }
            if (calculateShippingCost) {
                if (otr.getLineId() == null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error(otr.getGetUnit() + "未设置所属线路!");
                }
            }
            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setTransportNumber(otr.getTransportNumber());
            List<OrderTransportDetailedResult> list = orderTransportDetailedService.findListBySpec(otdp);//订单明细
            for (OrderTransportDetailedResult otdr : list) {
                BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
                BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
                BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//原数量
                BigDecimal num = new BigDecimal(otdr.getNotnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配数量
                BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
                BigDecimal money = num.multiply(unitprice).setScale(2, BigDecimal.ROUND_HALF_DOWN);//分配金额
                if (ynum.compareTo(num) == 0) {
                    volume = new BigDecimal(otdr.getTransportPutvolume());
                    money = new BigDecimal(otdr.getTransportMoney());
                }
                if (num.compareTo(as) == 0) {
                    continue;
                }

                //修改拣货单  已配置车牌号
                if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                    if (otr.getOrderNumber() != null) {
                        StorageOutParam sop = new StorageOutParam();
                        sop.setStorageOutNumber(otr.getCustomerOrder());
                        sop.setOutOrderStatus("revoke");
                        try {
                            StorageOutResult sor = storageOutService.findBySpec(sop);
                            if (sor != null) {
                                StorageOutParam sopupdate = new StorageOutParam();
                                if (sor.getPrivatecarCarnumbers() != null) {
                                    if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                        sopupdate.setId(sor.getId());
                                        sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                        storageOutService.update(sopupdate);
                                    }
                                } else {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                toovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
                toonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量
                BigDecimal totalNum = new BigDecimal(otr.getTotalNum());
                if (toonum.compareTo(totalNum) == 1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("超出订单商品体积");
                }
                if (totalNum.compareTo(toonum) == 0) {
                    boolean flag = basicPrivatecarService.findTwoByOrder(otp, null);//判断 配了几个车
                    if (!flag) {
                        otp.setTstate("已发运");
                    }
                }

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
                BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("超出商品体积");
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 输入类
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }

                //新增调度明细
                TransportDispatchParam tpdp = new TransportDispatchParam();
                tpdp.setDispatchNumber(dispatchNumber);
                tpdp.setTransportNumber(otr.getTransportNumber());
                tpdp.setCustomerOrder(otr.getCustomerOrder());
                tpdp.setCreateUserName(user.getName());
                tpdp.setBatchnumber(otdr.getBatchnumber());
                tpdp.setDetailedDate(otdr.getDetailedDate());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("零脉")) {
                        tpdp.setPositionId(otdr.getPositionId());
                    }
                }
                tpdp.setGoodsId(otdr.getGoodId());
                tpdp.setCarId(tDispatchResult.getCarId());
                tpdp.setCarnumbger(carnumbger);
                tpdp.setCartel(bpr.getTel1());
                tpdp.setDriver1(bpr.getDriver1());
                TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
                //判断给此车 是否分配过此订单的 商品
                if (tdr2 != null) {
                    BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                    BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                    BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                    tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                    tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                    BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tdr2.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                                }
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                                BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                                BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setJibenNum(tooAddjiben + "");
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        }
                    }
                    tdr2.setTransportMoney(transportMoney.add(money).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(tdr2, tpdp);
                    transportDispatchService.update(tpdp);
                } else {
                    tpdp.setTooDeployVolume(volume.toString());//分配的体积
                    tpdp.setTooDeployNum(num.toString());//分配的数量
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tpdp.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tpdp.setMatrixingNumber(num.toString());
                                }
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                tpdp.setJibenUnit(otdr.getJibenUnit());
                                tpdp.setJibenNum(otdr.getJibenNum());
                                tpdp.setDetailedDate(otdr.getDetailedDate());
                                tpdp.setDaoqiDate(otdr.getDaoqiDate());
                                tpdp.setXlbPositionId(otdr.getXlbPositionId());
                                tpdp.setPositionId(otdr.getPositionId());
                                tpdp.setPositionNumber(otdr.getPositionNumber());
                                tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                                tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                                tpdp.setGoodsUnit(otdr.getGoodsUnit());
                                tpdp.setRate(otdr.getRate());
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                        }
                    }
                    tpdp.setTransportMoney(money.toString());//金额
                    tpdp.setUnitprice(unitprice.toString());//单价
                    tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                    tpdp.setGoodsNumber(otdr.getGoodsNumber());
                    tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                    tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                    tpdp.setGoodsPacking(otdr.getDpacking());//包装
                    tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                    this.transportDispatchService.add(tpdp);
                }
            }
            otp.setId(otr.getId());
            otp.setTooVolume(toovolume.toString());
            otp.setTooNum(toonum.toString());
            orderTransportService.update(otp);
        }

        //重新计算派车单信息
        TransportReceiptParam receiptParam = new TransportReceiptParam();
        receiptParam.setDispatchNumber(tDispatchResult.getDispatchNumber());
        transportReceiptService.deleteBySpec(receiptParam);//删除回传表信息

        //添加回传单
        OrderTransportParam oParam = new OrderTransportParam();
        oParam.setDeptId(user.getDeptId());
        oParam.setOrderNumber(tDispatchResult.getDispatchNumber());
        List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo(oParam, tDispatchResult.getCarId(), null, user.getOperateId());
        int deliverys = 1;
        String getUnits = "";
        for (int i = 0; i < list2.size(); i++) {
            OrderTransportResult orderTransportResult = list2.get(i);
            TransportReceiptParam param = new TransportReceiptParam();
            param.setDispatchNumber(tDispatchResult.getDispatchNumber());
            param.setCarId(tDispatchResult.getCarId());
            param.setCarnumber(tDispatchResult.getCarnumber());
            param.setSetUnit(orderTransportResult.getSetUnit());
            param.setSetLocation(orderTransportResult.getSetLocation());
            param.setGetPeople(orderTransportResult.getGetPeople());
            param.setGetTel(orderTransportResult.getGetTel());
            param.setGetUnitid(orderTransportResult.getGetUnitId());
            param.setGetUnit(orderTransportResult.getGetUnit());
            getUnits += orderTransportResult.getGetUnit();
            if ((i + 1) != list2.size()) {
                getUnits += "+";
            }
            param.setGetLocation(orderTransportResult.getGetLocation());
            param.setGetLocationid(orderTransportResult.getGetLocationId());
            param.setSdtstate("未送达");
            param.setTstate("未回传");
            param.setNum(orderTransportResult.getTotalNum());
            param.setVolume(orderTransportResult.getTooVolume());
            param.setDeptId(user.getDeptId());
            param.setDeliverId(user.getDeliverId());
            param.setSetUnitId(orderTransportResult.getSetUnitId());
            param.setSetLocationId(orderTransportResult.getSetLocationId());
            param.setStockPort(orderTransportResult.getStockPort());
            param.setDeliverys(deliverys + "");
            param.setXlbClientId(orderTransportResult.getXlbClientId());
            deliverys++;
            transportReceiptService.add(param);
        }
        if (list2.size() == 1) {
            tDispatchResult.setMileageTraveled(list2.get(0).getKilometer());
        } else {
            tDispatchResult.setMileageTraveled("0");
        }
        tDispatchResult.setGetUnits(getUnits);

        TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
        transportDispatchParam.setDispatchNumber(tDispatchResult.getDispatchNumber());
        transportDispatchParam.setCarId(tDispatchResult.getCarId());
        transportDispatchParam.setDeptId(deptId);
        List<TransportDispatchResult> dlist = transportDispatchService.findOrderListBySpec(transportDispatchParam, null, user.getOperateId());
        BigDecimal toonum1 = new BigDecimal(0);
        BigDecimal toovolume1 = new BigDecimal(0);
        BigDecimal toomoney1 = new BigDecimal(0);

        String transportNumber = "";
        String customerOrder = "";
        List<String> line = new ArrayList<String>();
        for (TransportDispatchResult dr : dlist) {
            //合计数量体积
            BigDecimal tooDeployNum1 = new BigDecimal(dr.getTooDeployNum());
            BigDecimal transportMoney1 = new BigDecimal(dr.getTransportMoney());
            BigDecimal tooDeployVolume1 = new BigDecimal(dr.getTooDeployVolume());
            toonum1 = toonum1.add(tooDeployNum1).setScale(3, BigDecimal.ROUND_HALF_DOWN);
            toomoney1 = toomoney1.add(transportMoney1).setScale(2, BigDecimal.ROUND_HALF_DOWN);
            toovolume1 = toovolume1.add(tooDeployVolume1).setScale(4, BigDecimal.ROUND_HALF_DOWN);

            //查询货运订单
            OrderTransportParam orderTransportParam = new OrderTransportParam();
            orderTransportParam.setTransportNumber(dr.getTransportNumber());
            OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

            //新增运输费用
            if (orderTransportResult.getLineId() != null) {
                BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                if (basicLine != null) {
                    if (basicLine.getStatus().equals("1")) {
                        boolean yesflag = false;
                        for (String str : line) {
                            if (str.equals(basicLine.getLineName())) {
                                yesflag = true;
                                break;
                            }
                        }
                        if (!yesflag) {
                            line.add(basicLine.getLineName());
                        }
                    }
                }
            }

            if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                transportNumber += orderTransportResult.getTransportNumber() + ";";
            }
            if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                customerOrder += orderTransportResult.getCustomerOrder() + ";";
            }
        }

        //计算满载率
        BigDecimal arr = new BigDecimal(100);
        BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
        BigDecimal fulllv = toovolume1.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
        tDispatchResult.setFulllv(fulllv.toString());

        //更新运输单
        String lineName = "";
        if (line.size() == 1) {
            lineName = line.get(0);
        }
        if (line.size() > 1) {
            for (String str : line) {
                lineName += str + ",";
            }
        }
        tDispatchResult.setLineName(lineName);
        if (transportNumber.length() < 250) {
            tDispatchResult.setTransportNumber(transportNumber);
        }
        if (customerOrder.length() < 250) {
            tDispatchResult.setCustomerOrder(customerOrder);
        }
        tDispatchResult.setTooNum(toonum1.toString());
        tDispatchResult.setToovolume(toovolume1.toString());
        tDispatchResult.setToomoney(toomoney1.toString());
        ToolUtil.copyProperties(tDispatchResult, tDispatchParam);
        tDispatchService.update(tDispatchParam);
        return ResponseData.success();
    }

    @Override
    public List<TransportDispatchResult> findListByCarIsNull(TransportDispatchParam param) {
        return this.baseMapper.findListByCarIsNull(param);
    }

    @Override
    public List<TransportDispatchResult> findOrderListBySpec(TransportDispatchParam param,
                                                             String token, Long operateId) {
        return this.baseMapper.findOrderListBySpec(param, token, operateId);
    }

    @Override
    public List<TransportDispatchResult> findOrderListBySpecNew(TransportDispatchParam param,
                                                                String token, Long operateId) {
        return this.baseMapper.findOrderListBySpecNew(param, token, operateId);
    }

    /**
     * 移除订单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData removeOrderNew(String transportNumber, String carnumbger, ResponseData data) {
        User user = userAop.getUser();
        Long deliverId = user.getDeliverId();
        Long deptId = user.getDeptId();
        TransportDispatchParam tdp = new TransportDispatchParam();
        tdp.setCarnumbger(carnumbger);
        tdp.setTransportNumber(transportNumber);
        tdp.setDispatchNumber("yichu");
        tdp.setDeptId(deptId);
        List<TransportDispatchResult> list = transportDispatchService.findgoodslistBySpec(tdp, null, null, "Wait", null);
        for (TransportDispatchResult tdr : list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setGoodId(tdr.getGoodsId());
            otdp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

            BasicPrivatecarParam bpp = new BasicPrivatecarParam();
            bpp.setPrivatecarCarnumber(carnumbger);
            bpp.setDeliverId(deliverId);
            BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp, deptId, user.getOperateId());//车辆

            //调度明细
            BigDecimal tooDeployVolume = new BigDecimal(tdr.getTooDeployVolume());//配置体积
            BigDecimal tooDeployNum = new BigDecimal(tdr.getTooDeployNum());//配置数量

            //去除拣货单  已配置车牌号
            if (otr.getOrderNumber() != null) {
                StorageOutParam sop = new StorageOutParam();
                sop.setOutOrderNumber(otr.getOrderNumber());
                sop.setOutOrderStatus("revoke");
                StorageOutResult sor = storageOutService.findBySpec(sop);
                if (sor != null) {
                    if (sor.getPrivatecarCarnumbers() != null) {
                        sor.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(carnumbger + ";", ""));
                    }
                    ToolUtil.copyProperties(sor, sop);
                    storageOutService.update(sop);
                }
            }

            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
            BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntoonum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
            BigDecimal as = new BigDecimal(0);
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                data.setMessage("超出商品体积");
                data.setSuccess(false);
                return data;
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }

            //修改车辆
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
            tDispatchParam.setDeptId(deptId);
            TDispatchResult tDispatchResult = tDispatchService.findByYunshuCar(tDispatchParam, user.getOperateId());
            if (tDispatchResult == null) {
                bpr.setPrivatecarState("空闲");
                ToolUtil.copyProperties(bpr, bpp);
                basicPrivatecarService.update(bpp);
            }

            //删除调度明细
            ToolUtil.copyProperties(tdr, tdp);
            transportDispatchService.delete(tdp);
        }
        return ResponseData.success();
    }

    /**
     * 移除商品
     */
    @Override
    public ResponseData removeGoods(String transportNumber, Long goodsId, String carnumbger, ResponseData data) {
        User user = userAop.getUser();
        Long deptId = user.getDeptId();

        TransportDispatchParam tdp = new TransportDispatchParam();
        tdp.setCarnumbger(carnumbger);
        tdp.setTransportNumber(transportNumber);
        tdp.setGoodsId(goodsId);
        tdp.setDispatchNumber("yichu");
        tdp.setDeptId(deptId);
        List<TransportDispatchResult> list = transportDispatchService.findgoodslistBySpec(tdp, null, null, "Wait", null);
        for (TransportDispatchResult tdr : list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setGoodId(tdr.getGoodsId());
            otdp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

            BasicPrivatecarParam bpp = new BasicPrivatecarParam();
            bpp.setPrivatecarCarnumber(carnumbger);
            BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp, deptId, user.getOperateId());//车辆

            //调度明细
            BigDecimal tooDeployVolume = new BigDecimal(tdr.getTooDeployVolume());//配置体积
            BigDecimal tooDeployNum = new BigDecimal(tdr.getTooDeployNum());//配置数量

            //去除拣货单  已配置车牌号
            if (otr.getOrderNumber() != null) {
                StorageOutParam sop = new StorageOutParam();
                sop.setOutOrderNumber(otr.getOrderNumber());
                sop.setOutOrderStatus("revoke");
                StorageOutResult sor = storageOutService.findBySpec(sop);
                if (sor != null) {
                    if (sor.getPrivatecarCarnumbers() != null) {
                        sor.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(carnumbger + ";", ""));
                    }
                    ToolUtil.copyProperties(sor, sop);
                    storageOutService.update(sop);
                }
            }

            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
            BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntoonum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
            BigDecimal as = new BigDecimal(0);
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                data.setMessage("超出商品体积");
                data.setSuccess(false);
                return data;
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }
            //修改车辆
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
            tDispatchParam.setDeptId(deptId);
            TDispatchResult tDispatchResult = tDispatchService.findByYunshuCar(tDispatchParam, user.getOperateId());
            if (tDispatchResult == null) {
                bpr.setPrivatecarState("空闲");
                ToolUtil.copyProperties(bpr, bpp);
                basicPrivatecarService.update(bpp);
            }

            //删除调度明细
            ToolUtil.copyProperties(tdr, tdp);
            transportDispatchService.delete(tdp);
        }
        return ResponseData.success();
    }

    /**
     * 移除商品
     */
    @Override
    public ResponseData removeGoodsWeb(String transportNumber, Long goodsId, String carnumbger, ResponseData data,
                                       User user) {
        Long deptId = user.getDeptId();

        TransportDispatchParam tdp = new TransportDispatchParam();
        tdp.setCarnumbger(carnumbger);
        tdp.setTransportNumber(transportNumber);
        tdp.setGoodsId(goodsId);
        tdp.setDispatchNumber("yichu");
        tdp.setDeptId(deptId);
        List<TransportDispatchResult> list = transportDispatchService.findgoodslistBySpec(tdp, null, null, "Wait", null);
        for (TransportDispatchResult tdr : list) {
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setGoodId(tdr.getGoodsId());
            otdp.setTransportNumber(tdr.getTransportNumber());
            OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(otdp);//订单明细

            BasicPrivatecarParam bpp = new BasicPrivatecarParam();
            bpp.setPrivatecarCarnumber(carnumbger);
            BasicPrivatecarResult bpr = basicPrivatecarService.findBySpec(bpp, deptId, user.getOperateId());//车辆

            //调度明细
            BigDecimal tooDeployVolume = new BigDecimal(tdr.getTooDeployVolume());//配置体积
            BigDecimal tooDeployNum = new BigDecimal(tdr.getTooDeployNum());//配置数量

            //去除拣货单  已配置车牌号
            if (otr.getOrderNumber() != null) {
                StorageOutParam sop = new StorageOutParam();
                sop.setOutOrderNumber(otr.getOrderNumber());
                sop.setOutOrderStatus("revoke");
                StorageOutResult sor = storageOutService.findBySpec(sop);
                if (sor != null) {
                    if (sor.getPrivatecarCarnumbers() != null) {
                        sor.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers().replaceAll(carnumbger + ";", ""));
                    }
                    ToolUtil.copyProperties(sor, sop);
                    storageOutService.update(sop);
                }
            }

            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            BigDecimal toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            BigDecimal ntoovolume = toovolume.subtract(tooDeployVolume);//已分配体积 - 减少体积
            BigDecimal ntoonum = toonum.subtract(tooDeployNum);//已分配数量 - 减少数量
            otr.setTooVolume(ntoovolume.toString());
            otr.setTooNum(ntoonum.toString());
            ToolUtil.copyProperties(otr, otp);
            orderTransportService.update(otp);

            //修改订单明细  未分配体积,未分配数量
            BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
            BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
            BigDecimal newnotvolume = notvolume.add(tooDeployVolume);//未分配体积 + 减少体积 = 新未分配体积
            BigDecimal newnotnum = notnum.add(tooDeployNum);//未分配数量 -  减少数量 = 新未分配数量
            BigDecimal as = new BigDecimal(0);
            if (newnotnum.compareTo(as) == -1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                data.setMessage("超出商品体积");
                data.setSuccess(false);
                return data;
            } else {
                otdr.setNotnum(newnotnum.toString());
                otdr.setNotvolume(newnotvolume.toString());
                ToolUtil.copyProperties(otdr, otdp);
                orderTransportDetailedService.update(otdp);
            }
            //修改车辆
            TDispatchParam tDispatchParam = new TDispatchParam();
            tDispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
            tDispatchParam.setDeptId(deptId);
            TDispatchResult tDispatchResult = tDispatchService.findByYunshuCar(tDispatchParam, user.getOperateId());
            if (tDispatchResult == null) {
                bpr.setPrivatecarState("空闲");
                ToolUtil.copyProperties(bpr, bpp);
                basicPrivatecarService.update(bpp);
            }

            //删除调度明细
            ToolUtil.copyProperties(tdr, tdp);
            transportDispatchService.delete(tdp);
        }
        return ResponseData.success();
    }

    @Override
    public void updateDispatchNumber(TransportDispatchParam param, Long operateId) {
        this.baseMapper.updateDispatchNumber(param, operateId);
    }

    @Override
    public List<TransportDispatchResult> findbydispatchmumber(TransportDispatchParam orderTransportParam) {
        return this.baseMapper.findbydispatchmumber(orderTransportParam);
    }

    /**
     * 通过调度单号，收货单位id，收货地址id
     * 合计数量，体积
     * 按货运订单分组
     *
     * @param param
     * @param getUnitId
     * @param getLocationId
     * @return
     */
    @Override
    public List<TransportDispatchResult> sumBySpec(TransportDispatchParam param, Long getUnitId, Long getLocationId) {
        return this.baseMapper.sumBySpec(param, getUnitId, getLocationId);
    }

    @Override
    public List<TransportDispatchResult> findNotByCarnumber(TransportDispatchParam param) {
        return this.baseMapper.findNotByCarnumber(param);
    }

    @Override
    public LayuiPageInfo findCarListBySpecTooPage(TransportDispatchParam param, String dispatch, Long operateId) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findCarListBySpecTooPage(pageContext, param, dispatch, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<TransportDispatchResult> findPrintlistSpec(TransportDispatchParam param, TransportReceiptParam param2) {
        return this.baseMapper.findPrintlistSpec(param, param2);
    }

    @Override
    public List<ZhuangyunMingxiTongji> findchengyunshangMingxiCount(TransportDispatchParam param, String condition, String getUnit, String times
            , Long operateId, Long operateId2) {
        return this.baseMapper.findchengyunshangMingxiCount(param, condition, getUnit, times, operateId, operateId2);
    }

    @Override
    public List<ZhuangyunMingxiTongji> findchengyunshangMingxiAll(TransportDispatchParam param, String condition, String getUnit, String times
            , Long operateId, Long operateId2) {

        List<ZhuangyunMingxiTongji> zhuangyunMingxiTongjis = this.baseMapper.findchengyunshangMingxiAll(param, condition, getUnit, times, operateId, operateId2);

        String dispatchNumber = "";
        for (ZhuangyunMingxiTongji r : zhuangyunMingxiTongjis) {
            if (ToolUtil.isEmpty(dispatchNumber)) {
                dispatchNumber = r.getDispatchNumber();
                r.setDispatchNumber2(r.getDispatchNumber());
            } else {
                if (!dispatchNumber.equals(r.getDispatchNumber())) {
                    dispatchNumber = r.getDispatchNumber();
                    r.setDispatchNumber2(r.getDispatchNumber());
                } else {
                    r.setDispatchNumber("");
                    r.setDispatchNumber2(dispatchNumber);

                    r.setTrainNumber("");
                    r.setCarnumber("");
                    r.setTooNum("");
                    r.setMatrixingNumberSum("");
                    r.setToovolume("");
                    r.setToomoney("");
                    r.setMoney("");
                    r.setFulllv("");
                    r.setFayunTimes("");
                    r.setTel1("");
                    r.setDriver1("");
                    r.setPrivatecarType("");
                    r.setNotes("");
                    r.setMileageTraveled("");
                    r.setLoadingType("");
                    r.setLoadingPersonnel("");
                    r.setPrivatecarSurevolume("");
                    r.setChengyunfangshi("");
                    r.setChengyunleixing("");
                    r.setCarrierName("");
                    r.setXiechefei("");
                    r.setLoadingMoney("");
                    r.setFreightMoney("");
                    r.setConsultMoney("");
                }
            }
        }
        for (ZhuangyunMingxiTongji r : zhuangyunMingxiTongjis) {
            if (ToolUtil.isNotEmpty(r.getDispatchNumber())) {
                BigDecimal pieceNumberSum = new BigDecimal("0");
                BigDecimal matrixingNumberSum = new BigDecimal("0");
                for (ZhuangyunMingxiTongji r2 : zhuangyunMingxiTongjis) {
                    if (r.getDispatchNumber().equals(r2.getDispatchNumber2())) {
                        if (ToolUtil.isNotEmpty(r2.getPieceNumber())) {
                            BigDecimal pieceNumber = new BigDecimal(r2.getPieceNumber());
                            pieceNumberSum = pieceNumberSum.add(pieceNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        }
                        if (ToolUtil.isNotEmpty(r2.getMatrixingNumber())) {
                            BigDecimal matrixingNumber = new BigDecimal(r2.getMatrixingNumber());
                            matrixingNumberSum = matrixingNumberSum.add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        }
                    }
                }
                r.setPieceNumberSum(pieceNumberSum + "");
                r.setMatrixingNumberSum(matrixingNumberSum + "");
            }
        }
        return zhuangyunMingxiTongjis;
    }

    @Override
    public LayuiPageInfo findchengyunshangMingxiPage(TransportDispatchParam param, String condition, String getUnit, String times
            , Long operateId, Long operateId2) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findchengyunshangMingxiPage(pageContext, param, condition, getUnit, times, operateId, operateId2);
        List<ZhuangyunMingxiTongji> list = page.getRecords();
        String dispatchNumber = "";
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isEmpty(dispatchNumber)) {
                dispatchNumber = r.getDispatchNumber();
                r.setDispatchNumber2(r.getDispatchNumber());
            } else {
                if (!dispatchNumber.equals(r.getDispatchNumber())) {
                    dispatchNumber = r.getDispatchNumber();
                    r.setDispatchNumber2(r.getDispatchNumber());
                } else {
                    r.setDispatchNumber("");
                    r.setDispatchNumber2(dispatchNumber);
                    r.setTrainNumber("");
                    r.setCarnumber("");
                    r.setTooNum("");
                    r.setMatrixingNumberSum("");
                    r.setToovolume("");
                    r.setToomoney("");
                    r.setMoney("");
                    r.setFulllv("");
                    r.setFayunTimes("");
                    r.setTel1("");
                    r.setDriver1("");
                    r.setPrivatecarType("");
                    r.setNotes("");
                    r.setMileageTraveled("");
                    r.setLoadingType("");
                    r.setLoadingPersonnel("");
                    r.setPrivatecarSurevolume("");
                    r.setChengyunfangshi("");
                    r.setChengyunleixing("");
                    r.setCarrierName("");
                    r.setXiechefei("");
                    r.setLoadingMoney("");
                    r.setFreightMoney("");
                    r.setConsultMoney("");
                }
            }
        }
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isNotEmpty(r.getDispatchNumber())) {
                BigDecimal pieceNumberSum = new BigDecimal("0");
                BigDecimal matrixingNumberSum = new BigDecimal("0");
                for (ZhuangyunMingxiTongji r2 : list) {
                    if (r.getDispatchNumber().equals(r2.getDispatchNumber2())) {
                        if (ToolUtil.isNotEmpty(r2.getPieceNumber())) {
                            BigDecimal pieceNumber = new BigDecimal(r2.getPieceNumber());
                            pieceNumberSum = pieceNumberSum.add(pieceNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        }
                        if (ToolUtil.isNotEmpty(r2.getMatrixingNumber())) {
                            BigDecimal matrixingNumber = new BigDecimal(r2.getMatrixingNumber());
                            matrixingNumberSum = matrixingNumberSum.add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        }
                    }
                }
                r.setPieceNumberSum(pieceNumberSum + "");
                r.setMatrixingNumberSum(matrixingNumberSum + "");
            }
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findCarrierRecDetailPage(TransportDispatchParam transportDispatchParam, String condition, String getUnit, String times, Long operateId, Long operateId2) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findCarrierRecDetailPage(pageContext, transportDispatchParam, condition, getUnit, times, operateId, operateId2);
        List<ZhuangyunMingxiTongji> list = page.getRecords();

        Map<String, BigDecimal> pieceNumberMap = new HashMap<>();
        Map<String, BigDecimal> matrixingNumberMap = new HashMap<>();

        String dispatchNumber = "";
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isEmpty(dispatchNumber)) {
                dispatchNumber = r.getDispatchNumber();
                if (ToolUtil.isNotEmpty(r.getPieceNumber())) {
                    BigDecimal pieceNumber = new BigDecimal(r.getPieceNumber());
                    pieceNumberMap.put(dispatchNumber, pieceNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                }
                if (ToolUtil.isNotEmpty(r.getMatrixingNumber())) {
                    BigDecimal matrixingNumber = new BigDecimal(r.getMatrixingNumber());
                    matrixingNumberMap.put(dispatchNumber, matrixingNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                }
            } else {
                if (dispatchNumber.equals(r.getDispatchNumber())) {
                    if (ToolUtil.isNotEmpty(r.getPieceNumber())) {
                        BigDecimal pieceNumber = new BigDecimal(r.getPieceNumber());
                        if (pieceNumberMap.get(dispatchNumber) == null || pieceNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            pieceNumberMap.put(dispatchNumber, pieceNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            pieceNumberMap.put(dispatchNumber, pieceNumberMap.get(dispatchNumber).add(pieceNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                    if (ToolUtil.isNotEmpty(r.getMatrixingNumber())) {
                        BigDecimal matrixingNumber = new BigDecimal(r.getMatrixingNumber());
                        if (matrixingNumberMap.get(dispatchNumber) == null || matrixingNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumberMap.get(dispatchNumber).add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                    r.setTrainNumber("");
                    r.setCarnumber("");
                    r.setTooNum("");
                    r.setMatrixingNumberSum("");
                    r.setToovolume("");
                    r.setToomoney("");
                    r.setMoney("");
                    r.setFulllv("");
                    r.setStartDate("");
                    r.setDaocangTimes("");
                    r.setFayunTimes("");
                    r.setTel1("");
                    r.setDriver1("");
                    r.setPrivatecarType("");
                    r.setMileageTraveled("");
                    r.setLoadingType("");
                    r.setLoadingPersonnel("");
                    r.setPrivatecarSurevolume("");
                    r.setOperateName("");
//					r.setLineName("");
                    r.setChengyunfangshi("");
                    r.setChengyunleixing("");
                    r.setCarrierName("");
                    r.setXiechefei("");
                    r.setLoadingMoney("");
                    r.setFreightMoney("");
                    r.setConsultMoney("");
                    r.setDispatchNumber("");
                } else {
                    dispatchNumber = r.getDispatchNumber();
                    if (ToolUtil.isNotEmpty(r.getPieceNumber())) {
                        BigDecimal pieceNumber = new BigDecimal(r.getPieceNumber());
                        if (pieceNumberMap.get(dispatchNumber) == null || pieceNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            pieceNumberMap.put(dispatchNumber, pieceNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            pieceNumberMap.put(dispatchNumber, pieceNumberMap.get(dispatchNumber).add(pieceNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                    if (ToolUtil.isNotEmpty(r.getMatrixingNumber())) {
                        BigDecimal matrixingNumber = new BigDecimal(r.getMatrixingNumber());
                        if (matrixingNumberMap.get(dispatchNumber) == null || matrixingNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumberMap.get(dispatchNumber).add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                }
            }
        }

        BigDecimal big_0 = new BigDecimal(0);
        BigDecimal big_30 = new BigDecimal(30);
        BigDecimal big_60 = new BigDecimal(60);
        BigDecimal big_90 = new BigDecimal(90);
        BigDecimal big_120 = new BigDecimal(120);
        BigDecimal big_150 = new BigDecimal(150);
        BigDecimal big_180 = new BigDecimal(180);
        BigDecimal big_210 = new BigDecimal(210);
        BigDecimal big_250 = new BigDecimal(250);
        BigDecimal big_300 = new BigDecimal(300);
        BigDecimal big_400 = new BigDecimal(400);
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isNotEmpty(r.getDispatchNumber())) {
                if (ToolUtil.isNotEmpty(r.getMileageTraveled())) {
                    BigDecimal mileageTraveled = new BigDecimal(r.getMileageTraveled());
                    if (mileageTraveled.compareTo(big_0) > -1 && mileageTraveled.compareTo(big_30) < 1) {
                        r.setMileageTraveledTyp("0-30");
                    } else if (mileageTraveled.compareTo(big_30) > 0 && mileageTraveled.compareTo(big_60) < 1) {
                        r.setMileageTraveledTyp("30-60");
                    } else if (mileageTraveled.compareTo(big_60) > 0 && mileageTraveled.compareTo(big_90) < 1) {
                        r.setMileageTraveledTyp("60-90");
                    } else if (mileageTraveled.compareTo(big_90) > 0 && mileageTraveled.compareTo(big_120) < 1) {
                        r.setMileageTraveledTyp("90-120");
                    } else if (mileageTraveled.compareTo(big_120) > 0 && mileageTraveled.compareTo(big_150) < 1) {
                        r.setMileageTraveledTyp("120-150");
                    } else if (mileageTraveled.compareTo(big_150) > 0 && mileageTraveled.compareTo(big_180) < 1) {
                        r.setMileageTraveledTyp("150-180");
                    } else if (mileageTraveled.compareTo(big_180) > 0 && mileageTraveled.compareTo(big_210) < 1) {
                        r.setMileageTraveledTyp("180-210");
                    } else if (mileageTraveled.compareTo(big_210) > 0 && mileageTraveled.compareTo(big_250) < 1) {
                        r.setMileageTraveledTyp("210-250");
                    } else if (mileageTraveled.compareTo(big_250) > 0 && mileageTraveled.compareTo(big_300) < 1) {
                        r.setMileageTraveledTyp("250-300");
                    } else if (mileageTraveled.compareTo(big_300) > 0 && mileageTraveled.compareTo(big_400) < 1) {
                        r.setMileageTraveledTyp("300-400");
                    } else if (mileageTraveled.compareTo(big_400) > 0) {
                        r.setMileageTraveledTyp("400以上");
                    }
                }
                if (pieceNumberMap.get(r.getDispatchNumber()) != null) {
                    r.setPieceNumberSum(pieceNumberMap.get(r.getDispatchNumber()) + "");
                }
                if (matrixingNumberMap.get(r.getDispatchNumber()) != null) {
                    r.setMatrixingNumberSum(matrixingNumberMap.get(r.getDispatchNumber()) + "");
                }
            }
        }


        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZhuangyunMingxiTongji> findCarrierRecDetailAll(TransportDispatchParam transportDispatchParam, String condition, String getUnit, String times, Long operateId, Long operateId2) {
        List<ZhuangyunMingxiTongji> list = this.baseMapper.findCarrierRecDetailAll(transportDispatchParam, condition, getUnit, times, operateId, operateId2);

        Map<String, BigDecimal> pieceNumberMap = new HashMap<>();
        Map<String, BigDecimal> matrixingNumberMap = new HashMap<>();

        String dispatchNumber = "";
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isEmpty(dispatchNumber)) {
                dispatchNumber = r.getDispatchNumber();
                if (ToolUtil.isNotEmpty(r.getPieceNumber())) {
                    BigDecimal pieceNumber = new BigDecimal(r.getPieceNumber());
                    pieceNumberMap.put(dispatchNumber, pieceNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                }
                if (ToolUtil.isNotEmpty(r.getMatrixingNumber())) {
                    BigDecimal matrixingNumber = new BigDecimal(r.getMatrixingNumber());
                    matrixingNumberMap.put(dispatchNumber, matrixingNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                }
            } else {
                if (dispatchNumber.equals(r.getDispatchNumber())) {
                    if (ToolUtil.isNotEmpty(r.getPieceNumber())) {
                        BigDecimal pieceNumber = new BigDecimal(r.getPieceNumber());
                        if (pieceNumberMap.get(dispatchNumber) == null || pieceNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            pieceNumberMap.put(dispatchNumber, pieceNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            pieceNumberMap.put(dispatchNumber, pieceNumberMap.get(dispatchNumber).add(pieceNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                    if (ToolUtil.isNotEmpty(r.getMatrixingNumber())) {
                        BigDecimal matrixingNumber = new BigDecimal(r.getMatrixingNumber());
                        if (matrixingNumberMap.get(dispatchNumber) == null || matrixingNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumberMap.get(dispatchNumber).add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                    r.setTrainNumber("");
                    r.setCarnumber("");
                    r.setTooNum("");
                    r.setMatrixingNumberSum("");
                    r.setToovolume("");
                    r.setToomoney("");
                    r.setMoney("");
                    r.setFulllv("");
                    r.setStartDate("");
                    r.setDaocangTimes("");
                    r.setFayunTimes("");
                    r.setTel1("");
                    r.setDriver1("");
                    r.setPrivatecarType("");
                    r.setMileageTraveled("");
                    r.setLoadingType("");
                    r.setLoadingPersonnel("");
                    r.setPrivatecarSurevolume("");
                    r.setOperateName("");
//					r.setLineName("");
                    r.setChengyunfangshi("");
                    r.setChengyunleixing("");
                    r.setCarrierName("");
                    r.setXiechefei("");
                    r.setLoadingMoney("");
                    r.setFreightMoney("");
                    r.setConsultMoney("");
                    r.setDispatchNumber("");
                } else {
                    dispatchNumber = r.getDispatchNumber();
                    if (ToolUtil.isNotEmpty(r.getPieceNumber())) {
                        BigDecimal pieceNumber = new BigDecimal(r.getPieceNumber());
                        if (pieceNumberMap.get(dispatchNumber) == null || pieceNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            pieceNumberMap.put(dispatchNumber, pieceNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            pieceNumberMap.put(dispatchNumber, pieceNumberMap.get(dispatchNumber).add(pieceNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                    if (ToolUtil.isNotEmpty(r.getMatrixingNumber())) {
                        BigDecimal matrixingNumber = new BigDecimal(r.getMatrixingNumber());
                        if (matrixingNumberMap.get(dispatchNumber) == null || matrixingNumberMap.get(dispatchNumber).doubleValue() == 0) {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumber.setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        } else {
                            matrixingNumberMap.put(dispatchNumber, matrixingNumberMap.get(dispatchNumber).add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        }
                    }
                }
            }
        }

        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isNotEmpty(r.getDispatchNumber())) {
                if (pieceNumberMap.get(r.getDispatchNumber()) != null) {
                    r.setPieceNumberSum(pieceNumberMap.get(r.getDispatchNumber()) + "");
                }
                if (matrixingNumberMap.get(r.getDispatchNumber()) != null) {
                    r.setMatrixingNumberSum(matrixingNumberMap.get(r.getDispatchNumber()) + "");
                }
            }
        }
        return list;
    }

    @Override
    public LayuiPageInfo findCarrierRecFeePage(TransportDispatchParam transportDispatchParam, String condition, String times, Long operateId, Long operateId2) {

        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findCarrierRecFeePage(pageContext, transportDispatchParam, condition, times, operateId, operateId2);

        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findTmpCarAppExa(TransportDispatchParam transportDispatchParam, String condition, String times, Long operateId, Long operateId2) {

        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findTmpCarAppExa(pageContext, transportDispatchParam, condition, times, operateId, operateId2);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findTmpCarTb(TransportDispatchParam transportDispatchParam, String condition, String times, Long operateId, Long operateId2) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findTmpCarTb(pageContext, transportDispatchParam, condition, times, operateId, operateId2);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZhuangyunMingxiTongji> findCarrierRecFeeAll(TransportDispatchParam transportDispatchParam, String condition, String times, Long operateId, Long operateId2) {
        List<ZhuangyunMingxiTongji> list = this.baseMapper.findCarrierRecFeeAll(transportDispatchParam, condition, times, operateId, operateId2);
        return list;
    }

    @Override
    public List<ZhuangyunMingxiTongji> findchengyunshangGetUnitCount(TransportDispatchParam param, String condition, String getUnit, String times
            , Long operateId, Long operateId2) {
        return this.baseMapper.findchengyunshangGetUnitCount(param, condition, getUnit, times, operateId, operateId2);
    }

    @Override
    public LayuiPageInfo findchengyunshangGetUnitCountPage(TransportDispatchParam transportDispatchParam, String condition, String getUnit, String times, Long operateId, Long operateId2) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findchengyunshangGetUnitCountPage(pageContext, transportDispatchParam, condition, getUnit, times, operateId, operateId2);
        List<ZhuangyunMingxiTongji> list = page.getRecords();

        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isNotEmpty(r.getDispatchNumber())) {
                BigDecimal matrixingNumberSum = new BigDecimal("0");
                if (ToolUtil.isNotEmpty(r.getReachTime()) && r.getReachTime().split(" ").length > 1) {
                    String str0 = r.getReachTime().split(" ")[0];
                    String str1 = r.getReachTime().split(" ")[1];
                    r.setReachrq(str0);
                    r.setReachTime(str1);
                }
                for (ZhuangyunMingxiTongji r2 : list) {
                    if (r.getDispatchNumber().equals(r2.getDispatchNumber())) {
                        if (ToolUtil.isNotEmpty(r2.getMatrixingNumber())) {
                            BigDecimal matrixingNumber = new BigDecimal(r2.getMatrixingNumber());
                            matrixingNumberSum = matrixingNumberSum.add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        }
                    }
                }
                r.setMatrixingNumberSum(matrixingNumberSum + "");
            }
        }

        String dispatchNumber = "";
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isEmpty(dispatchNumber)) {
                dispatchNumber = r.getDispatchNumber();
            } else {
                if (dispatchNumber.equals(r.getDispatchNumber())) {
                    r.setTrainNumber("");
                    r.setCarnumber("");
                    r.setTooNum("");
                    r.setMatrixingNumberSum("");
                    r.setToovolume("");
                    r.setToomoney("");
                    r.setMoney("");
                    r.setFulllv("");
                    r.setStartDate("");
                    r.setDaocangTimes("");
                    r.setFayunTimes("");
                    r.setTel1("");
                    r.setDriver1("");
                    r.setPrivatecarType("");
                    r.setMileageTraveled("");
                    r.setLoadingType("");
                    r.setLoadingPersonnel("");
                    r.setPrivatecarSurevolume("");
                    r.setOperateName("");
                    r.setLineName("");
                    r.setChengyunfangshi("");
                    r.setChengyunleixing("");
                    r.setCarrierName("");
                    r.setXiechefei("");
                    r.setLoadingMoney("");
                    r.setFreightMoney("");
                    r.setConsultMoney("");
                    r.setDispatchNumber("");
                } else {
                    dispatchNumber = r.getDispatchNumber();
                }
            }
        }


        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZhuangyunMingxiTongji> findchengyunshangGetUnitCountAll(TransportDispatchParam transportDispatchParam, String condition, String getUnit, String times, Long operateId, Long operateId2) {
        List<ZhuangyunMingxiTongji> list = this.baseMapper.findchengyunshangGetUnitCount(transportDispatchParam, condition, getUnit, times, operateId, operateId2);
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isNotEmpty(r.getDispatchNumber())) {
                if (ToolUtil.isNotEmpty(r.getReachTime()) && r.getReachTime().split(" ").length > 1) {
                    String str0 = r.getReachTime().split(" ")[0];
                    String str1 = r.getReachTime().split(" ")[1];
                    r.setReachrq(str0);
                    r.setReachTime(str1);
                }
                BigDecimal matrixingNumberSum = new BigDecimal("0");
                for (ZhuangyunMingxiTongji r2 : list) {
                    if (r.getDispatchNumber().equals(r2.getDispatchNumber())) {
                        if (ToolUtil.isNotEmpty(r2.getMatrixingNumber())) {
                            BigDecimal matrixingNumber = new BigDecimal(r2.getMatrixingNumber());
                            matrixingNumberSum = matrixingNumberSum.add(matrixingNumber).setScale(2, BigDecimal.ROUND_HALF_DOWN);
                        }
                    }
                }
                r.setMatrixingNumberSum(matrixingNumberSum + "");
            }
        }
        String dispatchNumber = "";
        for (ZhuangyunMingxiTongji r : list) {
            if (ToolUtil.isEmpty(dispatchNumber)) {
                dispatchNumber = r.getDispatchNumber();
            } else {
                if (dispatchNumber.equals(r.getDispatchNumber())) {
                    r.setTrainNumber("");
                    r.setCarnumber("");
                    r.setTooNum("");
                    r.setMatrixingNumberSum("");
                    r.setToovolume("");
                    r.setToomoney("");
                    r.setMoney("");
                    r.setFulllv("");
                    r.setStartDate("");
                    r.setDaocangTimes("");
                    r.setFayunTimes("");
                    r.setTel1("");
                    r.setDriver1("");
                    r.setPrivatecarType("");
                    r.setMileageTraveled("");
                    r.setLoadingType("");
                    r.setLoadingPersonnel("");
                    r.setPrivatecarSurevolume("");
                    r.setOperateName("");
                    r.setLineName("");
                    r.setChengyunfangshi("");
                    r.setChengyunleixing("");
                    r.setCarrierName("");
                    r.setXiechefei("");
                    r.setLoadingMoney("");
                    r.setFreightMoney("");
                    r.setConsultMoney("");
                    r.setDispatchNumber("");
                } else {
                    dispatchNumber = r.getDispatchNumber();
                }
            }
        }

        return list;
    }

    @Override
    public TransportDispatchResult sumBySpec2(OrderTransportParam param, Long getLocationId) {
        return this.baseMapper.sumBySpec2(param, getLocationId);
    }

    @Override
    public TransportDispatchResult findBySpecNotEqCustomerOrder(TransportDispatchParam param) {
        return this.baseMapper.findBySpecNotEqCustomerOrder(param);
    }

    @Override
    public TransportDispatchResult findBySpecDispatchNumberIsnull(TransportDispatchParam param) {
        return this.baseMapper.findBySpecDispatchNumberIsnull(param);
    }

    @Override
    public TransportDispatchResult findByCarIsNull(TransportDispatchParam param, Long operateId) {
        return this.baseMapper.findByCarIsNull(param, operateId);
    }

    @Override
    public List<TransportDispatchResult> findgoodslistBySpecWeb(TransportDispatchParam transportDispatchParam,
                                                                String token, Long operateId) {
        return this.baseMapper.findgoodslistBySpecWeb(transportDispatchParam, token, operateId);
    }

    @Resource
    private DeptService deptService;

    /**
     * 获取所属项目单号前缀
     *
     * @param user
     * @return
     */
    public String getOperateIdOrderPrefix(User user) {
        if (user.getOperateId() != null) {
            Dept dept = new Dept();
            dept.setOperateId(user.getOperateId());
            Dept resdept = deptService.findBySpec(dept);
            if (resdept != null) {
                if (ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
                    return resdept.getOrderPrefix();
                } else {
                    return user.getAccount().substring(0, 3);
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData startCarGoods(TransportDispatchParam transportDispatchParam) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        String transportNumber = "";
        String customerOrder = "";

        transportDispatchParam.setDeptId(deptId);
        List<TransportDispatchResult> dlist = transportDispatchService.findOrderListBySpec(transportDispatchParam, "Wait", user.getOperateId());
        BigDecimal toonum = new BigDecimal(0);
        BigDecimal toovolume = new BigDecimal(0);
        BigDecimal toomoney = new BigDecimal(0);
        BigDecimal consultMoney = new BigDecimal(0);

        BasicPrivatecar basicPrivatecar = basicPrivatecarService.getById(transportDispatchParam.getCarId());

        String trayCode = getOperateIdOrderPrefix(user);
        //生成运输单号
        TDispatchParam dispatchParam = new TDispatchParam();
        dispatchParam.setDianduyuan(user.getName());
        dispatchParam.setChengyunfangshi("整车");
        dispatchParam.setPayee(basicPrivatecar.getPayee());
        dispatchParam.setBank(basicPrivatecar.getBank());
        dispatchParam.setBankAddress(basicPrivatecar.getBankAddress());
        dispatchParam.setBankAccount(basicPrivatecar.getBankAccount());
        dispatchParam.setBankNo(basicPrivatecar.getBankNo());
        dispatchParam.setCycle(basicPrivatecar.getCycle());
        dispatchParam.setOilCardNumber(basicPrivatecar.getOilCardNumber());
        dispatchParam.setPaiType("web");
        TDispatch tDispatch = this.tDispatchService.findByThelastone();
        if (tDispatch != null) {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-" + (tDispatch.getId() + 1));
        } else {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-1");
        }

        //添加回传单
        OrderTransportParam oParam = new OrderTransportParam();
        oParam.setDeptId(deptId);
        oParam.setTstate("dispatch");
        List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo(oParam, transportDispatchParam.getCarId(), null, user.getOperateId());
        int deliverys = 1;
        String getUnits = "";
        for (int i = 0; i < list2.size(); i++) {
            OrderTransportResult orderTransportResult = list2.get(i);
            TransportReceiptParam param = new TransportReceiptParam();
            param.setDispatchNumber(dispatchParam.getDispatchNumber());
            param.setCarId(transportDispatchParam.getCarId());
            param.setCarnumber(basicPrivatecar.getPrivatecarCarnumber());
            param.setSetUnit(orderTransportResult.getSetUnit());
            param.setSetLocation(orderTransportResult.getSetLocation());
            param.setGetPeople(orderTransportResult.getGetPeople());
            param.setGetTel(orderTransportResult.getGetTel());
            param.setGetUnitid(orderTransportResult.getGetUnitId());
            param.setGetUnit(orderTransportResult.getGetUnit());
            getUnits += orderTransportResult.getGetUnit();
            if ((i + 1) != list2.size()) {
                getUnits += "+";
            }
            param.setGetLocation(orderTransportResult.getGetLocation());
            param.setGetLocationid(orderTransportResult.getGetLocationId());
            param.setSdtstate("未送达");
            param.setTstate("未回传");
            param.setNum(orderTransportResult.getTotalNum());
            param.setVolume(orderTransportResult.getTooVolume());
            param.setDeptId(deptId);
            param.setDeliverId(deliverId);
            param.setSetUnitId(orderTransportResult.getSetUnitId());
            param.setSetLocationId(orderTransportResult.getSetLocationId());
            param.setStockPort(orderTransportResult.getStockPort());
            param.setDeliverys(deliverys + "");
            param.setXlbClientId(orderTransportResult.getXlbClientId());
            deliverys++;
            transportReceiptService.add(param);
        }
        if (list2.size() == 1) {
            dispatchParam.setMileageTraveled(list2.get(0).getKilometer());
        }
        if (getUnits.length() < 300) {
            dispatchParam.setGetUnits(getUnits);
        }

        //调度单号 更新到调度明细里
        transportDispatchParam.setDispatchNumber(dispatchParam.getDispatchNumber());
        transportDispatchService.updateDispatchNumber(transportDispatchParam, user.getOperateId());

        List<TransportCostParam> costlist = new ArrayList<TransportCostParam>();
        List<String> line = new ArrayList<String>();
        for (TransportDispatchResult dr : dlist) {
            //合计数量体积
            BigDecimal tooDeployNum = new BigDecimal(dr.getTooDeployNum());
            BigDecimal tooDeployVolume = new BigDecimal(dr.getTooDeployVolume());
            BigDecimal transportMoney = new BigDecimal(dr.getTransportMoney());
            toonum = toonum.add(tooDeployNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
            toovolume = toovolume.add(tooDeployVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);
            toomoney = toomoney.add(transportMoney).setScale(2, BigDecimal.ROUND_HALF_DOWN);

            //查询货运订单
            OrderTransportParam orderTransportParam = new OrderTransportParam();
            orderTransportParam.setTransportNumber(dr.getTransportNumber());
            OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

            dispatchParam.setOperateId(orderTransportResult.getOperateId());
            dispatchParam.setOperateName(orderTransportResult.getOperateName());

            //新增运输费用
            if (orderTransportResult.getLineId() != null) {
                BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                if (basicLine != null) {
                    if (basicLine.getStatus().equals("1")) {
                        boolean yesflag = false;
                        for (String str : line) {
                            if (str.equals(basicLine.getLineName())) {
                                yesflag = true;
                                break;
                            }
                        }
                        if (!yesflag) {
                            line.add(basicLine.getLineName());
                        }
                        BasicLinePriceParam basicLinePriceParam = new BasicLinePriceParam();
                        basicLinePriceParam.setLineId(Long.parseLong(basicLine.getId().toString()));
                        List<BasicLinePriceResult> LinePricelist = basicLinePriceService.findListBySpec(basicLinePriceParam);
                        for (BasicLinePriceResult r : LinePricelist) {
                            BigDecimal as = new BigDecimal(0);
                            BigDecimal price = new BigDecimal(0);
                            if (ToolUtil.isNotEmpty(r.getPrice())) {
                                price = new BigDecimal(r.getPrice());
                            }
                            if (price.compareTo(as) == 1) {
                                TransportCostParam transportCostParam = new TransportCostParam();
                                BigDecimal money = new BigDecimal(0);
                                if (r.getPriceMode().equals("元/m³")) {
                                    money = price.multiply(tooDeployVolume).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                    transportCostParam.setNums(dr.getTooDeployVolume());
                                    transportCostParam.setMoney(money.toString());
                                    transportCostParam.setV2(money.toString());
                                }
                                consultMoney = consultMoney.add(money);
                                transportCostParam.setDispatchNumber(dispatchParam.getDispatchNumber());
                                transportCostParam.setV1(dr.getTransportNumber());
                                transportCostParam.setPriceName(r.getPriceName());
                                transportCostParam.setPriceMode(r.getPriceMode());
                                transportCostParam.setPrice(r.getPrice());
                                transportCostParam.setLineId(Long.parseLong(basicLine.getId().toString()));
                                transportCostParam.setLineName(basicLine.getLineName());
                                transportCostParam.setDeptId(user.getDeptId());
                                transportCostParam.setDeliverId(user.getDeliverId());
                                if (r.getPriceMode().equals("元/趟")) {
                                    money = price;
                                    consultMoney = consultMoney.add(money);
                                    transportCostParam.setNums(dr.getTooDeployVolume());
                                    transportCostParam.setMoney(money.toString());
                                    transportCostParam.setV2(money.toString());
                                    boolean flag = false;
                                    for (TransportCostParam tc : costlist) {
                                        if (r.getLineId().longValue() == tc.getLineId().longValue()) {
                                            flag = true;
                                            break;
                                        }
                                    }
                                    if (!flag) {
                                        costlist.add(transportCostParam);
                                    }

                                } else {
                                    transportCostService.add(transportCostParam);
                                }
                            }
                        }
                    }
                }
            } else {
                TransportCostParam transportCostParam = new TransportCostParam();
                transportCostParam.setNums(dr.getTooDeployVolume());
                transportCostParam.setMoney("0");
                transportCostParam.setV2("0");
                transportCostParam.setDispatchNumber(dispatchParam.getDispatchNumber());
                transportCostParam.setV1(dr.getTransportNumber());
                transportCostParam.setDeptId(user.getDeptId());
                transportCostParam.setDeliverId(user.getDeliverId());
                transportCostService.add(transportCostParam);
            }

            if ("待调".equals(orderTransportResult.getTstate())) {
                if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                    transportNumber += orderTransportResult.getTransportNumber() + ";";
                }
                if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                    customerOrder += orderTransportResult.getCustomerOrder() + ";";
                }

                BigDecimal TotalNum = new BigDecimal(orderTransportResult.getTotalNum());
                BigDecimal TootalNum = new BigDecimal(0);
                TransportDispatchParam param = new TransportDispatchParam();
                param.setTransportNumber(orderTransportResult.getTransportNumber());
                List<TransportDispatchResult> list = transportDispatchService.findDispatchListBySpec(param, null);
                for (TransportDispatchResult dispatchResult : list) {
                    BigDecimal num = new BigDecimal(dispatchResult.getTooDeployNum());
                    TootalNum = TootalNum.add(num);
                }
                if (TotalNum.compareTo(TootalNum) == 0) {//判断商品是否  已经全部配置
                    //判断 配了几个车
                    orderTransportParam.setDeliverId(deliverId);
                    orderTransportParam.setDeptId(deptId);
                    boolean flag = basicPrivatecarService.findTwoByOrder(orderTransportParam, null);
                    if (!flag) {
                        orderTransportParam.setId(orderTransportResult.getId());
                        orderTransportParam.setTstate("已发运");
                        orderTransportService.update(orderTransportParam);
                    }
                }
            }
        }
        //新增运输费用 按线路  每趟
        for (TransportCostParam tc : costlist) {
            transportCostService.add(tc);
        }

        //修改车状态 为在途
        BasicPrivatecarParam bpcp2 = new BasicPrivatecarParam();
        bpcp2.setPrivatecarState("在途");
        bpcp2.setId(basicPrivatecar.getId());
        basicPrivatecarService.update(bpcp2);

        //计算满载率
        BigDecimal arr = new BigDecimal(100);
        BigDecimal privatecarSurevolume = new BigDecimal(basicPrivatecar.getPrivatecarSurevolume());//准载体积
        BigDecimal fulllv = toovolume.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
        dispatchParam.setFulllv(fulllv.toString());

        String lineName = "";
        if (line.size() == 1) {
            lineName = line.get(0);
        }
        if (line.size() > 1) {
            for (String str : line) {
                lineName += str + ",";
            }
        }
        //新增运输单
        dispatchParam.setLoadingType("仓库");
        dispatchParam.setLineName(lineName);
        if (ToolUtil.isNotEmpty(basicPrivatecar.getPrivatecarFleetName())) {
            dispatchParam.setCarrierId(Long.parseLong(basicPrivatecar.getPrivatecarFleetId().toString()));
            dispatchParam.setCarrierName(basicPrivatecar.getPrivatecarFleetName());
        }
        dispatchParam.setCarId(transportDispatchParam.getCarId());
        dispatchParam.setChexing(basicPrivatecar.getPrivatecarType());
        dispatchParam.setZhunzailiang(basicPrivatecar.getPrivatecarSurevolume());
        dispatchParam.setSiji(basicPrivatecar.getDriver1());
        dispatchParam.setSijidianhua(basicPrivatecar.getTel1());

        if (transportNumber.length() < 250) {
            dispatchParam.setTransportNumber(transportNumber);
        }
        if (customerOrder.length() < 250) {
            dispatchParam.setCustomerOrder(customerOrder);
        }
        dispatchParam.setCarnumber(basicPrivatecar.getPrivatecarCarnumber());
        dispatchParam.setTooNum(toonum.toString());
        dispatchParam.setToovolume(toovolume.toString());
        dispatchParam.setToomoney(toomoney.toString());
        dispatchParam.setTstate("待定价");
        dispatchParam.setReturnTstate("待回单");
        dispatchParam.setConsultMoney(consultMoney.toString());
        dispatchParam.setFreightMoney(consultMoney.toString());
        dispatchParam.setDeptId(deptId);
        dispatchParam.setDeliverId(deliverId);
        tDispatchService.add(dispatchParam);
        return ResponseData.success();
    }

    @Resource
    private TransportStevedoreSigninService transportStevedoreSigninService;

    @Override
    public List<peisongfenxi> findPeisongfenxi(TransportDispatchParam transportDispatchParam, String condition,
                                               String getUnit, String times, Long operateId, Long operateId2) {
        return this.baseMapper.findPeisongfenxi(transportDispatchParam, condition, getUnit, times, operateId, operateId2);
    }

    @Override
    public LayuiPageInfo findPeisongfenxiPage(TransportDispatchParam transportDispatchParam, String condition,
                                              String getUnit, String times, Long operateId, Long operateId2) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPeisongfenxiPage(pageContext, transportDispatchParam, condition, getUnit, times, operateId, operateId2);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<TransportDispatchResult> findListBySpecXianggui(TransportDispatchParam param) {
        return this.baseMapper.findListBySpecXianggui(param);
    }

    @Override
    public List<TransportDispatchResult> findPrintlistBySpec(TransportReceiptParam transportReceiptParam) {
        return this.baseMapper.findPrintlistBySpec(transportReceiptParam);
    }

    @Override
    public TransportDispatchResult sumMatrixingNumberByCarrierId(TransportDispatchParam dispatchParam) {
        return this.baseMapper.sumMatrixingNumberByCarrierId(dispatchParam);
    }

    @Override
    public List<ZhuangyunMingxiTongji> findPeisongjindu(TransportDispatchParam transportDispatchParam, String condition,
                                                        String times, Long operateId, Long operateId2) {
        return this.baseMapper.findPeisongjindu(transportDispatchParam, condition, times, operateId, operateId2);
    }

    @Override
    public List<TransportDispatchResult> findClListBySpec(TransportDispatchParam clParam) {
        return this.baseMapper.findClListBySpec(clParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData batchassign_driver(List<OrderTransportDetailedParam> _list, Long carId,
                                           String loadingType, String[] loadingNumbers, User user, BasicOperate basicOperate, String scanDress) {
        ResponseData data = new ResponseData();
        if (_list.size() == 0) {
            return ResponseData.error("未获取到订单");
        }
        Long deliverId = basicOperate.getDeliverId();
        Long deptId = basicOperate.getDeptId();
        boolean calculateShippingCost = false;
        if (basicOperate != null && ToolUtil.isNotEmpty(basicOperate.getCalculateShippingCost())) {
            if (basicOperate.getCalculateShippingCost().equals("是")) {
                calculateShippingCost = true;
            }
        }

        String trayCode = "";
        Dept dept = new Dept();
        dept.setOperateId(Long.parseLong(basicOperate.getId().toString()));
        Dept resdept = deptService.findBySpec(dept);
        if (resdept != null) {
            if (ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
                trayCode = resdept.getOrderPrefix();
            } else {
                trayCode = user.getAccount().substring(0, 3);
            }
        }

        BasicPrivatecar bpr = basicPrivatecarService.getById(carId);//车辆
        if (bpr.getPrivatecarFleetId() == null) {
            return ResponseData.error("请联系管理员维护" + bpr.getPrivatecarCarnumber() + "所属承运商");
        }
        if (bpr.getDriverId1() == null) {
            return ResponseData.error("请联系管理员维护" + bpr.getPrivatecarCarnumber() + "司机信息");
        }
        String carnumbger = bpr.getPrivatecarCarnumber();

        //生成运输单号
        TDispatchParam dispatchParam = new TDispatchParam();
        dispatchParam.setLoadingType(loadingType);
        if (ToolUtil.isNotEmpty(loadingType)) {
            if (loadingType.equals("自装")) {
                dispatchParam.setLoadingPersonnel("自装");
            } else if (loadingType.equals("仓库")) {
                if (loadingNumbers != null) {
                    String sloadingPersonnel = "";
                    String loadingNumbersp = "";
                    for (int i = 0; i < loadingNumbers.length; i++) {
                        String loadingNumber = loadingNumbers[i];
                        if (ToolUtil.isNotEmpty(loadingNumber)) {
                            TransportStevedoreSigninParam stevedoreSigninParam = new TransportStevedoreSigninParam();
                            stevedoreSigninParam.setAuditBy(loadingNumber);
                            stevedoreSigninParam.setDeptId(basicOperate.getDeptId());
                            TransportStevedoreSigninResult signres = transportStevedoreSigninService.findBySpec(stevedoreSigninParam, Long.parseLong(basicOperate.getId().toString()));
                            if (signres != null) {
                                sloadingPersonnel += signres.getAuditTime();
                                loadingNumbersp += loadingNumber;
                                if (loadingNumbers.length != i + 1) {
                                    sloadingPersonnel += "+";
                                    loadingNumbersp += ",";
                                }
                            }
                        }
                    }
                    dispatchParam.setLoadingNumber(loadingNumbersp);
                    dispatchParam.setLoadingPersonnel(sloadingPersonnel);
                }
            }
        } else {
            dispatchParam.setLoadingType("仓库");
        }
        dispatchParam.setDianduyuan(user.getName());
        dispatchParam.setChengyunfangshi("整车");
        dispatchParam.setChengyunleixing(bpr.getChengyunleixing());
        dispatchParam.setPayee(bpr.getPayee());
        dispatchParam.setBank(bpr.getBank());
        dispatchParam.setBankAddress(bpr.getBankAddress());
        dispatchParam.setBankAccount(bpr.getBankAccount());
        dispatchParam.setBankNo(bpr.getBankNo());
        dispatchParam.setCycle(bpr.getCycle());
        dispatchParam.setOilCardNumber(bpr.getOilCardNumber());
        dispatchParam.setMileageTraveledYy("0");
        dispatchParam.setRemain("0");
        TDispatch tDispatch = this.tDispatchService.findByThelastone();
        if (tDispatch != null) {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-" + (tDispatch.getId() + 1));
        } else {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-1");
        }

        for (OrderTransportDetailedParam otr2 : _list) {
            if (otr2.getGoodId() != null && otr2.getGoodId() != 0L) {
                List<OrderTransportDetailedParam> _list2 = new ArrayList<OrderTransportDetailedParam>();
                _list2.add(otr2);
                try {
                    data = transportDispatchService.assignNew(_list2, carId, dispatchParam.getDispatchNumber(), data, user, calculateShippingCost);
                    if (!data.getSuccess()) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                        return data;
                    }
                } catch (Exception e) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("后台异常");
                }
                continue;
            }

            OrderTransportResult otr = new OrderTransportResult();
            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(otr2.getTransportNumber());
            if (ToolUtil.isNotEmpty(scanDress) && scanDress.equals("是")) {
                otr = orderTransportService.findByTransportNumberOrOrderNumber(otp);//货运订单
            } else {
                otr = orderTransportService.findBySpec(otp, null);
            }
            if (otr == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单不存在！");
            }
            Integer count = orderTransportService.lambdaQuery()
                    .eq(OrderTransport::getCustomerOrder, otr.getCustomerOrder())
                    .eq(OrderTransport::getOperateId,basicOperate.getId())
                    .count();
            if (count > 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据数据重复异常！请联系管理员");
            }
            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            if (!"待调".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单发生变动");
            }
            if (otr.getLineId() == null) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error(otr.getGetUnit() + "未设置所属线路!");
            }

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setTransportNumber(otr.getTransportNumber());
            List<OrderTransportDetailedResult> list = orderTransportDetailedService.findListBySpec(otdp);//订单明细
            BigDecimal as = new BigDecimal(0);
            for (OrderTransportDetailedResult otdr : list) {
                BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
                BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
                BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//数量
                BigDecimal num = new BigDecimal(otdr.getNotnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配数量
                BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
                BigDecimal money = num.multiply(unitprice).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配金额
                if (ynum.compareTo(num) == 0) {
                    volume = new BigDecimal(otdr.getTransportPutvolume());
                    money = new BigDecimal(otdr.getTransportMoney());
                }
                if (num.compareTo(as) == 0) {
                    continue;
                }

                //修改拣货单  已配置车牌号
                if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                    if (otr.getOrderNumber() != null) {
                        StorageOutParam sop = new StorageOutParam();
                        sop.setStorageOutNumber(otr.getCustomerOrder());
                        sop.setOutOrderStatus("revoke");
                        try {
                            StorageOutResult sor = storageOutService.findBySpec(sop);
                            if (sor != null) {
                                StorageOutParam sopupdate = new StorageOutParam();
                                if (sor.getPrivatecarCarnumbers() != null) {
                                    if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                        sopupdate.setId(sor.getId());
                                        sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                        storageOutService.update(sopupdate);
                                    }
                                } else {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                toovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
                toonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量
                //装车 删除绑定门店记录
//				try{
//					if(otr != null && ToolUtil.isNotEmpty(otr.getXlbClientId())){
//						transportPlateStoreMapper.deleteByPutGoodsId(otr.getXlbClientId());
//					}
//				}catch ( Exception e){
//					e.printStackTrace();
//				}

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
                BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    data.setMessage("超出商品体积");
                    data.setSuccess(false);
                    return data;
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 输入类
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }

                //新增调度明细
                TransportDispatchParam tpdp = new TransportDispatchParam();
                tpdp.setDispatchNumber(dispatchParam.getDispatchNumber());
                tpdp.setTransportNumber(otr.getTransportNumber());
                tpdp.setCustomerOrder(otr.getCustomerOrder());
                tpdp.setCreateUserName(user.getName());
                tpdp.setGoodsId(otdr.getGoodId());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("零脉")) {
                        tpdp.setPositionId(otdr.getPositionId());
                    }
                }
                tpdp.setBatchnumber(otdr.getBatchnumber());
                tpdp.setDetailedDate(otdr.getDetailedDate());
                tpdp.setCarId(carId);
                tpdp.setCarnumbger(carnumbger);
                tpdp.setCartel(bpr.getTel1());
                tpdp.setDriver1(bpr.getDriver1());
                TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
                //判断给此车 是否分配过此订单的 商品
                if (tdr2 != null) {
                    BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                    BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                    BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                    tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                    tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                    BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tdr2.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                                }
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                                BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                                BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setJibenNum(tooAddjiben + "");
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        }
                    }
                    tdr2.setTransportMoney(transportMoney.add(money).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(tdr2, tpdp);
                    transportDispatchService.update(tpdp);
                } else {
                    tpdp.setTransportNumber(otr.getTransportNumber());//货运订单号
                    tpdp.setCarnumbger(carnumbger);//车牌号
                    tpdp.setTooDeployVolume(volume.toString());//分配的体积
                    tpdp.setTooDeployNum(num.toString());//分配的数量
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tpdp.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tpdp.setMatrixingNumber(num.toString());
                                }
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                tpdp.setJibenUnit(otdr.getJibenUnit());
                                tpdp.setJibenNum(otdr.getJibenNum());
                                tpdp.setDetailedDate(otdr.getDetailedDate());
                                tpdp.setDaoqiDate(otdr.getDaoqiDate());
                                tpdp.setXlbPositionId(otdr.getXlbPositionId());
                                tpdp.setPositionId(otdr.getPositionId());
                                tpdp.setPositionNumber(otdr.getPositionNumber());
                                tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                                tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                                tpdp.setGoodsUnit(otdr.getGoodsUnit());
                                tpdp.setRate(otdr.getRate());
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                        }
                    }
                    tpdp.setTransportMoney(money.toString());//金额
                    tpdp.setUnitprice(unitprice.toString());//单价
                    tpdp.setGoodsId(otdr.getGoodId());//商品id
                    tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                    tpdp.setGoodsNumber(otdr.getGoodsNumber());
                    tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                    tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                    tpdp.setGoodsPacking(otdr.getDpacking());//包装
                    tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                    this.transportDispatchService.add(tpdp);
                }
            }
            OrderTransportParam otpupdate = new OrderTransportParam();//货运订单 输入类
            otpupdate.setId(otr.getId());
            otpupdate.setTooVolume(toovolume.toString());
            otpupdate.setTooNum(toonum.toString());
            orderTransportService.update(otpupdate);
        }


        String transportNumber = "";
        String customerOrder = "";
        TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
        transportDispatchParam.setDispatchNumber(dispatchParam.getDispatchNumber());
        transportDispatchParam.setCarId(carId);
        transportDispatchParam.setDeptId(basicOperate.getDeptId());
        List<TransportDispatchResult> dlist = transportDispatchService.findOrderListBySpec(transportDispatchParam, null, Long.parseLong(basicOperate.getId().toString()));
        BigDecimal toonum = new BigDecimal(0);
        BigDecimal toovolume = new BigDecimal(0);
        BigDecimal toomoney = new BigDecimal(0);

        //添加回传单
        OrderTransportParam oParam = new OrderTransportParam();
        oParam.setDeptId(basicOperate.getDeptId());
        oParam.setOrderNumber(dispatchParam.getDispatchNumber());
        List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo2(oParam, carId, Long.parseLong(basicOperate.getId().toString()));
        int deliverys = 1;
        String getUnits = "";
        for (int i = 0; i < list2.size(); i++) {
            OrderTransportResult orderTransportResult = list2.get(i);
            TransportReceiptParam param = new TransportReceiptParam();
            param.setDispatchNumber(dispatchParam.getDispatchNumber());
            param.setCarnumber(bpr.getPrivatecarCarnumber());
            param.setCarId(carId);
            param.setSetUnit(orderTransportResult.getSetUnit());
            param.setSetLocation(orderTransportResult.getSetLocation());
            param.setGetPeople(orderTransportResult.getGetPeople());
            param.setGetTel(orderTransportResult.getGetTel());
            param.setGetUnitid(orderTransportResult.getGetUnitId());
            param.setGetUnit(orderTransportResult.getGetUnit());
            getUnits += orderTransportResult.getGetUnit();
            if ((i + 1) != list2.size()) {
                getUnits += "+";
            }
            param.setGetLocation(orderTransportResult.getGetLocation());
            param.setGetLocationid(orderTransportResult.getGetLocationId());
            param.setSdtstate("未送达");
            param.setTstate("未回传");
            param.setNum(orderTransportResult.getTotalNum());
            param.setVolume(orderTransportResult.getTooVolume());
            param.setDeptId(deptId);
            param.setDeliverId(deliverId);
            param.setSetUnitId(orderTransportResult.getSetUnitId());
            param.setSetLocationId(orderTransportResult.getSetLocationId());
            param.setStockPort(orderTransportResult.getStockPort());
            param.setDeliverys(deliverys + "");
            param.setXlbClientId(orderTransportResult.getXlbClientId());
            deliverys++;
            transportReceiptService.add(param);
        }
        if (list2.size() == 1) {
            dispatchParam.setMileageTraveled(list2.get(0).getKilometer());
        } else {
            dispatchParam.setMileageTraveled("0");
        }
        if (getUnits.length() < 300) {
            dispatchParam.setGetUnits(getUnits);
        }

        //调度单号 更新到调度明细里
//    	transportDispatchParam.setDispatchNumber(dispatchParam.getDispatchNumber());
//    	transportDispatchService.updateDispatchNumber(transportDispatchParam,user.getOperateId());

        List<String> line = new ArrayList<String>();
        for (TransportDispatchResult dr : dlist) {
            //合计数量体积
            BigDecimal tooDeployNum = new BigDecimal(dr.getTooDeployNum());
            BigDecimal tooDeployVolume = new BigDecimal(dr.getTooDeployVolume());
            BigDecimal transportMoney = new BigDecimal(dr.getTransportMoney());
            toonum = toonum.add(tooDeployNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
            toovolume = toovolume.add(tooDeployVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);
            toomoney = toomoney.add(transportMoney).setScale(2, BigDecimal.ROUND_HALF_DOWN);

            //查询货运订单
            OrderTransportParam orderTransportParam = new OrderTransportParam();
            orderTransportParam.setTransportNumber(dr.getTransportNumber());
            OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

            dispatchParam.setOperateId(orderTransportResult.getOperateId());
            dispatchParam.setOperateName(orderTransportResult.getOperateName());

            //新增运输费用
            if (orderTransportResult.getLineId() != null) {
                BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                if (basicLine != null) {
                    if (basicLine.getStatus().equals("1")) {
                        boolean yesflag = false;
                        for (String str : line) {
                            if (str.equals(basicLine.getLineName())) {
                                yesflag = true;
                                break;
                            }
                        }
                        if (!yesflag) {
                            line.add(basicLine.getLineName());
                        }
                    }
                }
            }

            if ("待调".equals(orderTransportResult.getTstate())) {
                if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                    transportNumber += orderTransportResult.getTransportNumber() + ";";
                }
                if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                    customerOrder += orderTransportResult.getCustomerOrder() + ";";
                }

                BigDecimal TotalNum = new BigDecimal(orderTransportResult.getTotalNum());
                BigDecimal TootalNum = new BigDecimal(0);
                TransportDispatchParam param = new TransportDispatchParam();
                param.setTransportNumber(orderTransportResult.getTransportNumber());
                List<TransportDispatchResult> list = transportDispatchService.findDispatchListBySpec(param, null);
                for (TransportDispatchResult dispatchResult : list) {
                    BigDecimal num = new BigDecimal(dispatchResult.getTooDeployNum());
                    TootalNum = TootalNum.add(num);
                }
                if (TotalNum.compareTo(TootalNum) == 0) {//判断商品是否  已经全部配置
                    //判断 配了几个车
                    boolean flag = basicPrivatecarService.findTwoByOrder(orderTransportParam, null);
                    if (!flag) {
                        orderTransportParam.setId(orderTransportResult.getId());
                        orderTransportParam.setTstate("已发运");
                        orderTransportService.update(orderTransportParam);
                    }
                }
            }
        }

        //修改车状态 为在途
        BasicPrivatecarParam bpcp2 = new BasicPrivatecarParam();
        bpcp2.setPrivatecarState("在途");
        bpcp2.setId(bpr.getId());
        basicPrivatecarService.update(bpcp2);

        //计算满载率
        BigDecimal arr = new BigDecimal(100);
        BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
        BigDecimal fulllv = toovolume.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
        dispatchParam.setFulllv(fulllv.toString());

        String lineName = "";
        if (line.size() == 1) {
            lineName = line.get(0);
        }
        if (line.size() > 1) {
            for (String str : line) {
                lineName += str + ",";
            }
        }
        //新增运输单
        dispatchParam.setLineName(lineName);
        if (ToolUtil.isNotEmpty(bpr.getPrivatecarFleetName())) {
            dispatchParam.setCarrierId(Long.parseLong(bpr.getPrivatecarFleetId().toString()));
            dispatchParam.setCarrierName(bpr.getPrivatecarFleetName());
        }
        dispatchParam.setCarId(carId);
        dispatchParam.setChexing(bpr.getPrivatecarType());
        dispatchParam.setZhunzailiang(bpr.getPrivatecarSurevolume());
        dispatchParam.setSiji(bpr.getDriver1());
        dispatchParam.setSijidianhua(bpr.getTel1());

        if (transportNumber.length() < 250) {
            dispatchParam.setTransportNumber(transportNumber);
        }
        if (customerOrder.length() < 250) {
            dispatchParam.setCustomerOrder(customerOrder);
        }
        dispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
        dispatchParam.setTooNum(toonum.toString());
        dispatchParam.setToovolume(toovolume.toString());
        dispatchParam.setToomoney(toomoney.toString());
        dispatchParam.setTstate("待定价");
        dispatchParam.setReturnTstate("待回单");
        if (calculateShippingCost) {
            dispatchParam.setCalculateShippingCost("否");
        } else {
            dispatchParam.setCalculateShippingCost("是");
        }
        dispatchParam.setConsultMoney("0");
        dispatchParam.setFreightMoney("0");
        dispatchParam.setDeptId(deptId);
        dispatchParam.setDeliverId(deliverId);
        tDispatchService.add(dispatchParam);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData batchassign_app(List<OrderTransportDetailedParam> _list, Long carId, User user) {
        if (_list.size() == 0) {
            return ResponseData.error("未获取到订单");
        }
        ResponseData data = new ResponseData();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        boolean calculateShippingCost = false;
        if (user.getOperateId() != null) {
            BasicOperate basicOperate = basicOperateService.getById(user.getOperateId());
            if (basicOperate != null && ToolUtil.isNotEmpty(basicOperate.getCalculateShippingCost())) {
                if (basicOperate.getCalculateShippingCost().equals("是")) {
                    calculateShippingCost = true;
                }
            }
        }

        BasicPrivatecar bpr = basicPrivatecarService.getById(carId);//车辆
        if (bpr.getPrivatecarFleetId() == null) {
            return ResponseData.error("请维护" + bpr.getPrivatecarCarnumber() + "所属承运商");
        }
        if (bpr.getDriverId1() == null) {
            return ResponseData.error("请维护" + bpr.getPrivatecarCarnumber() + "司机信息");
        }
        String carnumbger = bpr.getPrivatecarCarnumber();

        String trayCode = getOperateIdOrderPrefix(user);
        //生成运输单号
        TDispatchParam dispatchParam = new TDispatchParam();
        dispatchParam.setLoadingType("仓库");
        dispatchParam.setDianduyuan(user.getName());
        dispatchParam.setChengyunfangshi("整车");
        dispatchParam.setChengyunleixing(bpr.getChengyunleixing());
        dispatchParam.setPayee(bpr.getPayee());
        dispatchParam.setBank(bpr.getBank());
        dispatchParam.setBankAddress(bpr.getBankAddress());
        dispatchParam.setBankAccount(bpr.getBankAccount());
        dispatchParam.setBankNo(bpr.getBankNo());
        dispatchParam.setCycle(bpr.getCycle());
        dispatchParam.setOilCardNumber(bpr.getOilCardNumber());
        dispatchParam.setMileageTraveledYy("0");
        dispatchParam.setRemain("0");
        TDispatch tDispatch = this.tDispatchService.findByThelastone();
        if (tDispatch != null) {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-" + (tDispatch.getId() + 1));
        } else {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-1");
        }

        for (OrderTransportDetailedParam otr2 : _list) {
            if (otr2.getGoodId() != null && otr2.getGoodId() != 0L) {
                List<OrderTransportDetailedParam> _list2 = new ArrayList<OrderTransportDetailedParam>();
                _list2.add(otr2);
                try {
                    data = transportDispatchService.assignNew(_list2, carId, dispatchParam.getDispatchNumber(), data, user, calculateShippingCost);
                    if (!data.getSuccess()) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                        return data;
                    }
                } catch (Exception e) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("后台异常");
                }
                continue;
            }

            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(otr2.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单
            Integer count = orderTransportService.lambdaQuery()
                    .eq(OrderTransport::getCustomerOrder, otr.getCustomerOrder())
                    .eq(OrderTransport::getOperateId,tDispatch.getOperateId())
                    .count();
            if (count > 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据数据重复异常！请联系管理员");
            }
            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            if (!"待调".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单发生变动");
            }
            if (calculateShippingCost) {
                if (otr.getLineId() == null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error(otr.getGetUnit() + "未设置所属线路!");
                }
            }

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setTransportNumber(otr.getTransportNumber());
            List<OrderTransportDetailedResult> list = orderTransportDetailedService.findListBySpec(otdp);//订单明细
            BigDecimal as = new BigDecimal(0);
            for (OrderTransportDetailedResult otdr : list) {
                BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
                BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
                BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//数量
                BigDecimal num = new BigDecimal(otdr.getNotnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配数量
                BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
                BigDecimal money = num.multiply(unitprice).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配金额
                if (ynum.compareTo(num) == 0) {
                    volume = new BigDecimal(otdr.getTransportPutvolume());
                    money = new BigDecimal(otdr.getTransportMoney());
                }
                if (num.compareTo(as) == 0) {
                    continue;
                }

                //修改拣货单  已配置车牌号
                if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                    if (otr.getOrderNumber() != null) {
                        StorageOutParam sop = new StorageOutParam();
                        sop.setStorageOutNumber(otr.getCustomerOrder());
                        sop.setOutOrderStatus("revoke");
                        try {
                            StorageOutResult sor = storageOutService.findBySpec(sop);
                            if (sor != null) {
                                StorageOutParam sopupdate = new StorageOutParam();
                                if (sor.getPrivatecarCarnumbers() != null) {
                                    if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                        sopupdate.setId(sor.getId());
                                        sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                        storageOutService.update(sopupdate);
                                    }
                                } else {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                toovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
                toonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
                BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    data.setMessage("超出商品体积");
                    data.setSuccess(false);
                    return data;
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 输入类
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }

                //新增调度明细
                TransportDispatchParam tpdp = new TransportDispatchParam();
                tpdp.setDispatchNumber(dispatchParam.getDispatchNumber());
                tpdp.setTransportNumber(otr.getTransportNumber());
                tpdp.setCustomerOrder(otr.getCustomerOrder());
                tpdp.setCreateUserName(user.getName());
                tpdp.setGoodsId(otdr.getGoodId());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("零脉")) {
                        tpdp.setPositionId(otdr.getPositionId());
                    }
                }
                tpdp.setBatchnumber(otdr.getBatchnumber());
                tpdp.setDetailedDate(otdr.getDetailedDate());
                tpdp.setCarId(carId);
                tpdp.setCarnumbger(carnumbger);
                tpdp.setCartel(bpr.getTel1());
                tpdp.setDriver1(bpr.getDriver1());
                TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
                //判断给此车 是否分配过此订单的 商品
                if (tdr2 != null) {
                    BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                    BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                    BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                    tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                    tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                    BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tdr2.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                                }
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                                BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                                BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setJibenNum(tooAddjiben + "");
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        }
                    }
                    tdr2.setTransportMoney(transportMoney.add(money).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(tdr2, tpdp);
                    transportDispatchService.update(tpdp);
                } else {
                    tpdp.setTransportNumber(otr.getTransportNumber());//货运订单号
                    tpdp.setCarnumbger(carnumbger);//车牌号
                    tpdp.setTooDeployVolume(volume.toString());//分配的体积
                    tpdp.setTooDeployNum(num.toString());//分配的数量
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tpdp.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tpdp.setMatrixingNumber(num.toString());
                                }
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                tpdp.setJibenUnit(otdr.getJibenUnit());
                                tpdp.setJibenNum(otdr.getJibenNum());
                                tpdp.setDetailedDate(otdr.getDetailedDate());
                                tpdp.setDaoqiDate(otdr.getDaoqiDate());
                                tpdp.setXlbPositionId(otdr.getXlbPositionId());
                                tpdp.setPositionId(otdr.getPositionId());
                                tpdp.setPositionNumber(otdr.getPositionNumber());
                                tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                                tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                                tpdp.setGoodsUnit(otdr.getGoodsUnit());
                                tpdp.setRate(otdr.getRate());
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                        }
                    }
                    tpdp.setTransportMoney(money.toString());//金额
                    tpdp.setUnitprice(unitprice.toString());//单价
                    tpdp.setGoodsId(otdr.getGoodId());//商品id
                    tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                    tpdp.setGoodsNumber(otdr.getGoodsNumber());
                    tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                    tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                    tpdp.setGoodsPacking(otdr.getDpacking());//包装
                    tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                    this.transportDispatchService.add(tpdp);
                }
            }
            OrderTransportParam otpupdate = new OrderTransportParam();
            otpupdate.setId(otr.getId());
            otpupdate.setTooVolume(toovolume.toString());
            otpupdate.setTooNum(toonum.toString());
            orderTransportService.update(otpupdate);
        }

        String transportNumber = "";
        String customerOrder = "";
        TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
        transportDispatchParam.setDispatchNumber(dispatchParam.getDispatchNumber());
        transportDispatchParam.setCarId(carId);
        transportDispatchParam.setDeptId(deptId);
        List<TransportDispatchResult> dlist = transportDispatchService.findOrderListBySpec(transportDispatchParam, null, user.getOperateId());
        BigDecimal toonum = new BigDecimal(0);
        BigDecimal toovolume = new BigDecimal(0);
        BigDecimal toomoney = new BigDecimal(0);

        //添加回传单
        OrderTransportParam oParam = new OrderTransportParam();
        oParam.setDeptId(deptId);
        oParam.setOrderNumber(dispatchParam.getDispatchNumber());
        List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo2(oParam, carId, user.getOperateId());
        int deliverys = 1;
        String getUnits = "";
        for (int i = 0; i < list2.size(); i++) {
            OrderTransportResult orderTransportResult = list2.get(i);
            TransportReceiptParam param = new TransportReceiptParam();
            param.setDispatchNumber(dispatchParam.getDispatchNumber());
            param.setCarnumber(bpr.getPrivatecarCarnumber());
            param.setCarId(carId);
            param.setSetUnit(orderTransportResult.getSetUnit());
            param.setSetLocation(orderTransportResult.getSetLocation());
            param.setGetPeople(orderTransportResult.getGetPeople());
            param.setGetTel(orderTransportResult.getGetTel());
            param.setGetUnitid(orderTransportResult.getGetUnitId());
            param.setGetUnit(orderTransportResult.getGetUnit());
            getUnits += orderTransportResult.getGetUnit();
            if ((i + 1) != list2.size()) {
                getUnits += "+";
            }
            param.setGetLocation(orderTransportResult.getGetLocation());
            param.setGetLocationid(orderTransportResult.getGetLocationId());
            param.setSdtstate("未送达");
            param.setTstate("未回传");
            param.setNum(orderTransportResult.getTotalNum());
            param.setVolume(orderTransportResult.getTooVolume());
            param.setDeptId(deptId);
            param.setDeliverId(deliverId);
            param.setSetUnitId(orderTransportResult.getSetUnitId());
            param.setSetLocationId(orderTransportResult.getSetLocationId());
            param.setStockPort(orderTransportResult.getStockPort());
            param.setDeliverys(deliverys + "");
            param.setXlbClientId(orderTransportResult.getXlbClientId());
            deliverys++;
            transportReceiptService.add(param);
        }
        if (list2.size() == 1) {
            dispatchParam.setMileageTraveled(list2.get(0).getKilometer());
        } else {
            dispatchParam.setMileageTraveled("0");
        }
        if (getUnits.length() < 300) {
            dispatchParam.setGetUnits(getUnits);
        }

        //调度单号 更新到调度明细里
//    	transportDispatchParam.setDispatchNumber(dispatchParam.getDispatchNumber());
//    	transportDispatchService.updateDispatchNumber(transportDispatchParam,user.getOperateId());

        List<String> line = new ArrayList<String>();
        for (TransportDispatchResult dr : dlist) {
            //合计数量体积
            BigDecimal tooDeployNum = new BigDecimal(dr.getTooDeployNum());
            BigDecimal tooDeployVolume = new BigDecimal(dr.getTooDeployVolume());
            BigDecimal transportMoney = new BigDecimal(dr.getTransportMoney());
            toonum = toonum.add(tooDeployNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
            toovolume = toovolume.add(tooDeployVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);
            toomoney = toomoney.add(transportMoney).setScale(2, BigDecimal.ROUND_HALF_DOWN);

            //查询货运订单
            OrderTransportParam orderTransportParam = new OrderTransportParam();
            orderTransportParam.setTransportNumber(dr.getTransportNumber());
            OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

            dispatchParam.setOperateId(orderTransportResult.getOperateId());
            dispatchParam.setOperateName(orderTransportResult.getOperateName());

            //新增运输费用
            if (orderTransportResult.getLineId() != null) {
                BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                if (basicLine != null) {
                    if (basicLine.getStatus().equals("1")) {
                        boolean yesflag = false;
                        for (String str : line) {
                            if (str.equals(basicLine.getLineName())) {
                                yesflag = true;
                                break;
                            }
                        }
                        if (!yesflag) {
                            line.add(basicLine.getLineName());
                        }
                    }
                }
            }

            if ("待调".equals(orderTransportResult.getTstate())) {
                if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                    transportNumber += orderTransportResult.getTransportNumber() + ";";
                }
                if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                    customerOrder += orderTransportResult.getCustomerOrder() + ";";
                }

                BigDecimal TotalNum = new BigDecimal(orderTransportResult.getTotalNum());
                BigDecimal TootalNum = new BigDecimal(0);
                TransportDispatchParam param = new TransportDispatchParam();
                param.setTransportNumber(orderTransportResult.getTransportNumber());
                List<TransportDispatchResult> list = transportDispatchService.findDispatchListBySpec(param, null);
                for (TransportDispatchResult dispatchResult : list) {
                    BigDecimal num = new BigDecimal(dispatchResult.getTooDeployNum());
                    TootalNum = TootalNum.add(num);
                }
                if (TotalNum.compareTo(TootalNum) == 0) {//判断商品是否  已经全部配置
                    //判断 配了几个车
                    orderTransportParam.setDeliverId(deliverId);
                    orderTransportParam.setDeptId(deptId);
                    boolean flag = basicPrivatecarService.findTwoByOrder(orderTransportParam, null);
                    if (!flag) {
                        orderTransportParam.setId(orderTransportResult.getId());
                        orderTransportParam.setTstate("已发运");
                        orderTransportService.update(orderTransportParam);
                    }
                }
            }
        }

        //修改车状态 为在途
        BasicPrivatecarParam bpcp2 = new BasicPrivatecarParam();
        bpcp2.setPrivatecarState("在途");
        bpcp2.setId(bpr.getId());
        basicPrivatecarService.update(bpcp2);

        //计算满载率
        BigDecimal arr = new BigDecimal(100);
        BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
        BigDecimal fulllv = toovolume.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
        dispatchParam.setFulllv(fulllv.toString());

        String lineName = "";
        if (line.size() == 1) {
            lineName = line.get(0);
        }
        if (line.size() > 1) {
            for (String str : line) {
                lineName += str + ",";
            }
        }
        //新增运输单
        dispatchParam.setLineName(lineName);
        if (ToolUtil.isNotEmpty(bpr.getPrivatecarFleetName())) {
            dispatchParam.setCarrierId(Long.parseLong(bpr.getPrivatecarFleetId().toString()));
            dispatchParam.setCarrierName(bpr.getPrivatecarFleetName());
        }
        dispatchParam.setCarId(carId);
        dispatchParam.setChexing(bpr.getPrivatecarType());
        dispatchParam.setZhunzailiang(bpr.getPrivatecarSurevolume());
        dispatchParam.setSiji(bpr.getDriver1());
        dispatchParam.setSijidianhua(bpr.getTel1());

        if (transportNumber.length() < 250) {
            dispatchParam.setTransportNumber(transportNumber);
        }
        if (customerOrder.length() < 250) {
            dispatchParam.setCustomerOrder(customerOrder);
        }
        dispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
        dispatchParam.setTooNum(toonum.toString());
        dispatchParam.setToovolume(toovolume.toString());
        dispatchParam.setToomoney(toomoney.toString());
        dispatchParam.setTstate("待定价");
        dispatchParam.setReturnTstate("待回单");
        if (calculateShippingCost) {
            dispatchParam.setCalculateShippingCost("否");
        } else {
            dispatchParam.setCalculateShippingCost("是");
        }
        dispatchParam.setConsultMoney("0");
        dispatchParam.setFreightMoney("0");
        dispatchParam.setDeptId(deptId);
        dispatchParam.setDeliverId(deliverId);
        tDispatchService.add(dispatchParam);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, timeout = 300)
    public ResponseData batchassign_web(List<OrderTransportDetailedParam> _list, Long carId, User user) {
        if (_list.size() == 0) {
            return ResponseData.error("未获取到订单");
        }
        ResponseData data = new ResponseData();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        boolean calculateShippingCost = false;
        if (user.getOperateId() != null) {
            BasicOperate basicOperate = basicOperateService.getById(user.getOperateId());
            if (basicOperate != null && ToolUtil.isNotEmpty(basicOperate.getCalculateShippingCost())) {
                if (basicOperate.getCalculateShippingCost().equals("是")) {
                    calculateShippingCost = true;
                }
            }
        }

        BasicPrivatecar bpr = basicPrivatecarService.getById(carId);//车辆
        String roleId = user.getRoleId();
        if (ToolUtil.isNotEmpty(roleId)) {
            for (String rId : roleId.split(",")) {
                Role byId = roleService.getById(rId);
                if (byId != null && byId.getName().contains("承运商")) {
                    BasicCarrierParam param = new BasicCarrierParam();
                    param.setUserId(user.getUserId());
                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
                    if (bySpec != null) {
                        bpr.setPrivatecarFleetId(bySpec.getId());
                        bpr.setPrivatecarFleetName(bySpec.getCarrierName());
                    }
                }
            }
        }

        if (bpr.getPrivatecarFleetId() == null) {
            return ResponseData.error("请维护" + bpr.getPrivatecarCarnumber() + "所属承运商");
        }
        if (bpr.getDriverId1() == null) {
            return ResponseData.error("请维护" + bpr.getPrivatecarCarnumber() + "司机信息");
        }
        String carnumbger = bpr.getPrivatecarCarnumber();

        String trayCode = getOperateIdOrderPrefix(user);
        //生成运输单号
        TDispatchParam dispatchParam = new TDispatchParam();
        dispatchParam.setLoadingType("仓库");
        dispatchParam.setDianduyuan(user.getName());
        dispatchParam.setChengyunfangshi("整车");
        dispatchParam.setChengyunleixing(bpr.getChengyunleixing());
        dispatchParam.setPayee(bpr.getPayee());
        dispatchParam.setBank(bpr.getBank());
        dispatchParam.setBankAddress(bpr.getBankAddress());
        dispatchParam.setBankAccount(bpr.getBankAccount());
        dispatchParam.setBankNo(bpr.getBankNo());
        dispatchParam.setCycle(bpr.getCycle());
        dispatchParam.setOilCardNumber(bpr.getOilCardNumber());
        dispatchParam.setMileageTraveledYy("0");
        dispatchParam.setRemain("0");
        TDispatch tDispatch = this.tDispatchService.findByThelastone();
        if (tDispatch != null) {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-" + (tDispatch.getId() + 1));
        } else {
            dispatchParam.setDispatchNumber(trayCode + "-" + "DD-" + DateUtil.getDateBch() + "-1");
        }
        for (OrderTransportDetailedParam otr2 : _list) {
            if (otr2.getGoodId() != null && otr2.getGoodId() != 0L) {
                List<OrderTransportDetailedParam> _list2 = new ArrayList<OrderTransportDetailedParam>();
                _list2.add(otr2);
                try {
                    data = transportDispatchService.assignNew(_list2, carId, dispatchParam.getDispatchNumber(), data, user, calculateShippingCost);
                    if (!data.getSuccess()) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                        return data;
                    }
                } catch (Exception e) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error("后台异常");
                }
                continue;
            }

            OrderTransportParam otp = new OrderTransportParam();//货运订单 输入类
            otp.setTransportNumber(otr2.getTransportNumber());
            OrderTransportResult otr = orderTransportService.findBySpec(otp, null);//货运订单
            Integer count = orderTransportService.lambdaQuery()
                    .eq(OrderTransport::getCustomerOrder, otr.getCustomerOrder())
                    .eq(OrderTransport::getOperateId,tDispatch.getOperateId())
                    .count();
            if (count > 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("单据数据重复异常！请联系管理员");
            }
            //修改货物订单  已配置体积 已配置数量
            BigDecimal toovolume = new BigDecimal(0);//订单已配置体积
            BigDecimal toonum = new BigDecimal(0);//订单已配置数量
            if (ToolUtil.isNotEmpty(otr.getTooVolume())) {
                toovolume = new BigDecimal(otr.getTooVolume());//订单已配置体积
            }
            if (ToolUtil.isNotEmpty(otr.getTooNum())) {
                toonum = new BigDecimal(otr.getTooNum());//订单已配置数量
            }
            if (!"待调".equals(otr.getTstate())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                return ResponseData.error("订单发生变动");
            }
            if (calculateShippingCost) {
                if (otr.getLineId() == null) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    return ResponseData.error(otr.getGetUnit() + "未设置所属线路!");
                }
            }

            OrderTransportDetailedParam otdp = new OrderTransportDetailedParam();//订单明细 输入类
            otdp.setTransportNumber(otr.getTransportNumber());
            List<OrderTransportDetailedResult> list = orderTransportDetailedService.findListBySpec(otdp);//订单明细
            for (OrderTransportDetailedResult otdr : list) {
                BigDecimal goodsVolume = new BigDecimal(otdr.getGoodsVolume());//体积(m3)
                BigDecimal unitprice = new BigDecimal(otdr.getUnitprice());//单价
                BigDecimal ynum = new BigDecimal(otdr.getNum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//数量
                BigDecimal num = new BigDecimal(otdr.getNotnum()).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未配数量
                BigDecimal volume = num.multiply(goodsVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//分配体积
                BigDecimal money = num.multiply(unitprice).setScale(3, BigDecimal.ROUND_HALF_DOWN);//分配金额
                if (ynum.compareTo(num) == 0) {
                    volume = new BigDecimal(otdr.getTransportPutvolume());
                    money = new BigDecimal(otdr.getTransportMoney());
                }
                BigDecimal as = new BigDecimal(0);
                if (num.compareTo(as) == 0) {
                    continue;
                }

                //修改拣货单  已配置车牌号
                if (ToolUtil.isNotEmpty(otr.getOrderSource()) && otr.getOrderSource().equals("WMS")) {
                    if (otr.getOrderNumber() != null) {
                        StorageOutParam sop = new StorageOutParam();
                        sop.setStorageOutNumber(otr.getCustomerOrder());
                        sop.setOutOrderStatus("revoke");
                        try {
                            StorageOutResult sor = storageOutService.findBySpec(sop);
                            if (sor != null) {
                                StorageOutParam sopupdate = new StorageOutParam();
                                if (sor.getPrivatecarCarnumbers() != null) {
                                    if (sor.getPrivatecarCarnumbers().indexOf(carnumbger) == -1) {
                                        sopupdate.setId(sor.getId());
                                        sopupdate.setPrivatecarCarnumbers(sor.getPrivatecarCarnumbers() + carnumbger + ";");
                                        storageOutService.update(sopupdate);
                                    }
                                } else {
                                    sopupdate.setId(sor.getId());
                                    sopupdate.setPrivatecarCarnumbers(carnumbger + ";");
                                    storageOutService.update(sopupdate);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }

                toovolume = toovolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//已分配体积 + 新分配体积
                toonum = toonum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//已分配数量 + 新分配数量

                //修改订单明细  未分配体积,未分配数量
                BigDecimal notvolume = new BigDecimal(otdr.getNotvolume());//未分配体积
                BigDecimal notnum = new BigDecimal(otdr.getNotnum());//未分配数量
                BigDecimal newnotvolume = notvolume.subtract(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN);//未分配体积 - 新分配体积 = 新未分配体积
                BigDecimal newnotnum = notnum.subtract(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);//未分配数量 - 新分配数量 = 新未分配数量
                if (newnotnum.compareTo(as) == -1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动回滚失误
                    data.setMessage("超出商品体积");
                    data.setSuccess(false);
                    return data;
                } else {
                    OrderTransportDetailedParam otdpupdate = new OrderTransportDetailedParam();//订单明细 更新类
                    otdpupdate.setId(otdr.getId());
                    otdpupdate.setNotnum(newnotnum.toString());
                    otdpupdate.setNotvolume(newnotvolume.toString());
                    orderTransportDetailedService.update(otdpupdate);
                }

                //新增调度明细
                TransportDispatchParam tpdp = new TransportDispatchParam();
                tpdp.setDispatchNumber(dispatchParam.getDispatchNumber());
                tpdp.setTransportNumber(otr.getTransportNumber());
                tpdp.setCustomerOrder(otr.getCustomerOrder());
                tpdp.setCreateUserName(user.getName());
                tpdp.setGoodsId(otdr.getGoodId());
                tpdp.setBatchnumber(otdr.getBatchnumber());
                tpdp.setDetailedDate(otdr.getDetailedDate());
                if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                    if (otr.getOrderSource().equals("零脉")) {
                        tpdp.setPositionId(otdr.getPositionId());
                    }
                }
                tpdp.setCarId(carId);
                tpdp.setCarnumbger(carnumbger);
                tpdp.setCartel(bpr.getTel1());
                tpdp.setDriver1(bpr.getDriver1());
                TransportDispatchResult tdr2 = transportDispatchService.findBySpec(tpdp);
                //判断给此车 是否分配过此订单的 商品
                if (tdr2 != null) {
                    BigDecimal tooDeployVolume = new BigDecimal(tdr2.getTooDeployVolume());
                    BigDecimal tooDeployNum = new BigDecimal(tdr2.getTooDeployNum());
                    BigDecimal transportMoney = new BigDecimal(tdr2.getTransportMoney());
                    tdr2.setTooDeployVolume(tooDeployVolume.add(volume).setScale(4, BigDecimal.ROUND_HALF_DOWN).toString());
                    tdr2.setTooDeployNum(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                    BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tdr2.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                                }
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                BigDecimal JibenNumbig = new BigDecimal(otdr.getJibenNum());
                                BigDecimal tooJibenNumbig = new BigDecimal(tdr2.getJibenNum());
                                BigDecimal tooAddjiben = tooJibenNumbig.add(JibenNumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setJibenNum(tooAddjiben + "");
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal tooDeployNumadd = tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN);
                                BigDecimal matrixingNumber = tooDeployNumadd.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tdr2.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tdr2.setMatrixingNumber(tooDeployNum.add(num).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                            }
                        }
                    }
                    tdr2.setTransportMoney(transportMoney.add(money).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
                    ToolUtil.copyProperties(tdr2, tpdp);
                    transportDispatchService.update(tpdp);
                } else {
                    tpdp.setTransportNumber(otr.getTransportNumber());//货运订单号
                    tpdp.setCarnumbger(carnumbger);//车牌号
                    tpdp.setTooDeployVolume(volume.toString());//分配的体积
                    tpdp.setTooDeployNum(num.toString());//分配的数量
                    if (ToolUtil.isNotEmpty(otr.getOrderSource())) {
                        if (otr.getOrderSource().equals("新零帮") || otr.getOrderSource().equals("零脉")) {
                            if (ToolUtil.isNotEmpty(otr.getDemolition())) {
                                if (otr.getDemolition().equals("拆零")) {
                                    BigDecimal xianggui = new BigDecimal("10");
                                    BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                    tpdp.setMatrixingNumber(matrixingNumber.toString());
                                } else {
                                    tpdp.setMatrixingNumber(num.toString());
                                }
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                            if (otr.getOrderSource().equals("零脉")) {
                                tpdp.setJibenUnit(otdr.getJibenUnit());
                                tpdp.setJibenNum(otdr.getJibenNum());
                                tpdp.setDetailedDate(otdr.getDetailedDate());
                                tpdp.setDaoqiDate(otdr.getDaoqiDate());
                                tpdp.setXlbPositionId(otdr.getXlbPositionId());
                                tpdp.setPositionId(otdr.getPositionId());
                                tpdp.setPositionNumber(otdr.getPositionNumber());
                                tpdp.setXlbGoodsId(otdr.getXlbGoodsId());
                                tpdp.setGoodsPcode69(otdr.getGoodsPcode69());
                                tpdp.setGoodsUnit(otdr.getGoodsUnit());
                                tpdp.setRate(otdr.getRate());
                            }
                        }
                        if (otr.getOrderSource().equals("WMS")) {
                            BasicGoods basicGoods = basicGoodsService.getById(otdr.getGoodId());
                            if (ToolUtil.isNotEmpty(basicGoods.getXianggui())) {
                                BigDecimal xianggui = new BigDecimal(basicGoods.getXianggui());
                                BigDecimal matrixingNumber = num.divide(xianggui, 3, BigDecimal.ROUND_HALF_DOWN);
                                tpdp.setMatrixingNumber(matrixingNumber.toString());
                            } else {
                                tpdp.setMatrixingNumber(num.toString());
                            }
                        }
                    }
                    tpdp.setTransportMoney(money.toString());//金额
                    tpdp.setUnitprice(unitprice.toString());//单价
                    tpdp.setGoodsId(otdr.getGoodId());//商品id
                    tpdp.setGoodsName(otdr.getGoodsName());//商品名称
                    tpdp.setGoodsNumber(otdr.getGoodsNumber());
                    tpdp.setGoodsNo(otdr.getGoodsNo());//货号
                    tpdp.setGoodsVolume(otdr.getGoodsVolume());//体积
                    tpdp.setGoodsPacking(otdr.getDpacking());//包装
                    tpdp.setGoodsSpecs(otdr.getGoodsSpecs());//规格
                    this.transportDispatchService.add(tpdp);
                }
            }
            OrderTransportParam otpupdate = new OrderTransportParam();//货运订单 更新类
            otpupdate.setId(otr.getId());
            otpupdate.setTooVolume(toovolume.toString());
            otpupdate.setTooNum(toonum.toString());
            orderTransportService.update(otpupdate);
        }

        //添加回传单
        OrderTransportParam oParam = new OrderTransportParam();
        oParam.setDeptId(deptId);
        oParam.setOrderNumber(dispatchParam.getDispatchNumber());
        List<OrderTransportResult> list2 = orderTransportService.carDetailsSingleToo2(oParam, carId, user.getOperateId());
        int deliverys = 1;
        String getUnits = "";
        for (int i = 0; i < list2.size(); i++) {
            OrderTransportResult orderTransportResult = list2.get(i);
            TransportReceiptParam param = new TransportReceiptParam();
            param.setDispatchNumber(dispatchParam.getDispatchNumber());
            param.setCarnumber(bpr.getPrivatecarCarnumber());
            param.setCarId(carId);
            param.setSetUnit(orderTransportResult.getSetUnit());
            param.setSetLocation(orderTransportResult.getSetLocation());
            param.setGetPeople(orderTransportResult.getGetPeople());
            param.setGetTel(orderTransportResult.getGetTel());
            param.setGetUnitid(orderTransportResult.getGetUnitId());
            param.setGetUnit(orderTransportResult.getGetUnit());
            getUnits += orderTransportResult.getGetUnit();
            if ((i + 1) != list2.size()) {
                getUnits += "+";
            }
            param.setGetLocation(orderTransportResult.getGetLocation());
            param.setGetLocationid(orderTransportResult.getGetLocationId());
            param.setSdtstate("未送达");
            param.setTstate("未回传");
            param.setNum(orderTransportResult.getTotalNum());
            param.setVolume(orderTransportResult.getTooVolume());
            param.setDeptId(deptId);
            param.setDeliverId(deliverId);
            param.setSetUnitId(orderTransportResult.getSetUnitId());
            param.setSetLocationId(orderTransportResult.getSetLocationId());
            param.setStockPort(orderTransportResult.getStockPort());
            param.setDeliverys(deliverys + "");
            param.setXlbClientId(orderTransportResult.getXlbClientId());
            deliverys++;
            transportReceiptService.add(param);
        }
        if (list2.size() == 1) {
            dispatchParam.setMileageTraveled(list2.get(0).getKilometer());
        } else {
            dispatchParam.setMileageTraveled("0");
        }
        if (getUnits.length() < 300) {
            dispatchParam.setGetUnits(getUnits);
        }

        String transportNumber = "";
        String customerOrder = "";
        TransportDispatchParam transportDispatchParam = new TransportDispatchParam();
        transportDispatchParam.setDispatchNumber(dispatchParam.getDispatchNumber());
        transportDispatchParam.setCarId(carId);
        transportDispatchParam.setDeptId(deptId);
        List<TransportDispatchResult> dlist = transportDispatchService.findOrderListBySpec(transportDispatchParam, null, user.getOperateId());
        BigDecimal toonum = new BigDecimal(0);
        BigDecimal toovolume = new BigDecimal(0);
        BigDecimal toomoney = new BigDecimal(0);
        List<String> line = new ArrayList<String>();
        for (TransportDispatchResult dr : dlist) {
            //合计数量体积
            BigDecimal tooDeployNum = new BigDecimal(dr.getTooDeployNum());
            BigDecimal tooDeployVolume = new BigDecimal(dr.getTooDeployVolume());
            BigDecimal transportMoney = new BigDecimal(dr.getTransportMoney());
            toonum = toonum.add(tooDeployNum).setScale(3, BigDecimal.ROUND_HALF_DOWN);
            toovolume = toovolume.add(tooDeployVolume).setScale(4, BigDecimal.ROUND_HALF_DOWN);
            toomoney = toomoney.add(transportMoney).setScale(2, BigDecimal.ROUND_HALF_DOWN);

            //查询货运订单
            OrderTransportParam orderTransportParam = new OrderTransportParam();
            orderTransportParam.setTransportNumber(dr.getTransportNumber());
            OrderTransportResult orderTransportResult = orderTransportService.findBySpec(orderTransportParam, null);

            dispatchParam.setOperateId(orderTransportResult.getOperateId());
            dispatchParam.setOperateName(orderTransportResult.getOperateName());

            //新增运输费用
            if (orderTransportResult.getLineId() != null) {
                BasicLine basicLine = basicLineService.getById(orderTransportResult.getLineId());
                if (basicLine != null) {
                    if (basicLine.getStatus().equals("1")) {
                        //汇总线路 放到运输单里
                        boolean yesflag = false;
                        for (String str : line) {
                            if (str.equals(basicLine.getLineName())) {
                                yesflag = true;
                                break;
                            }
                        }
                        if (!yesflag) {
                            line.add(basicLine.getLineName());
                        }
                    }
                }
            }

            if ("待调".equals(orderTransportResult.getTstate())) {
                if (transportNumber.indexOf(orderTransportResult.getTransportNumber()) == -1) {
                    transportNumber += orderTransportResult.getTransportNumber() + ";";
                }
                if (customerOrder.indexOf(orderTransportResult.getCustomerOrder()) == -1) {
                    customerOrder += orderTransportResult.getCustomerOrder() + ";";
                }

                BigDecimal TotalNum = new BigDecimal(orderTransportResult.getTotalNum());
                BigDecimal TootalNum = new BigDecimal(0);
                TransportDispatchParam param = new TransportDispatchParam();
                param.setTransportNumber(orderTransportResult.getTransportNumber());
                List<TransportDispatchResult> list = transportDispatchService.findDispatchListBySpec(param, null);
                for (TransportDispatchResult dispatchResult : list) {
                    BigDecimal num = new BigDecimal(dispatchResult.getTooDeployNum());
                    TootalNum = TootalNum.add(num);
                }
                if (TotalNum.compareTo(TootalNum) == 0) {//判断商品是否  已经全部配置
                    //判断 配了几个车
                    boolean flag = basicPrivatecarService.findTwoByOrder(orderTransportParam, null);
                    if (!flag) {
                        orderTransportParam.setId(orderTransportResult.getId());
                        orderTransportParam.setTstate("已发运");
                        orderTransportService.update(orderTransportParam);
                    }
                }
            }
        }

        //修改车状态 为在途
        BasicPrivatecarParam bpcp2 = new BasicPrivatecarParam();
        bpcp2.setPrivatecarState("在途");
        bpcp2.setId(bpr.getId());
        basicPrivatecarService.update(bpcp2);

        //计算满载率
        BigDecimal arr = new BigDecimal(100);
        BigDecimal privatecarSurevolume = new BigDecimal(bpr.getPrivatecarSurevolume());//准载体积
        BigDecimal fulllv = toovolume.divide(privatecarSurevolume, 4, BigDecimal.ROUND_HALF_DOWN).multiply(arr).setScale(2, BigDecimal.ROUND_HALF_DOWN);//满载率  =  运输总方数   /   准载体积   *   100
        dispatchParam.setFulllv(fulllv.toString());

        String lineName = "";
        if (line.size() == 1) {
            lineName = line.get(0);
        }
        if (line.size() > 1) {
            for (String str : line) {
                lineName += str + ",";
            }
        }
        //新增运输单
        dispatchParam.setLineName(lineName);
        if (ToolUtil.isNotEmpty(bpr.getPrivatecarFleetName())) {
            dispatchParam.setCarrierId(Long.parseLong(bpr.getPrivatecarFleetId().toString()));
            dispatchParam.setCarrierName(bpr.getPrivatecarFleetName());
        }
        dispatchParam.setCarId(carId);
        dispatchParam.setChexing(bpr.getPrivatecarType());
        dispatchParam.setZhunzailiang(bpr.getPrivatecarSurevolume());
        dispatchParam.setSiji(bpr.getDriver1());
        dispatchParam.setSijidianhua(bpr.getTel1());

        if (transportNumber.length() < 250) {
            dispatchParam.setTransportNumber(transportNumber);
        }
        if (customerOrder.length() < 250) {
            dispatchParam.setCustomerOrder(customerOrder);
        }
        dispatchParam.setCarnumber(bpr.getPrivatecarCarnumber());
        dispatchParam.setTooNum(toonum.toString());
        dispatchParam.setToovolume(toovolume.toString());
        dispatchParam.setToomoney(toomoney.toString());
        dispatchParam.setTstate("待定价");
        dispatchParam.setReturnTstate("待回单");
        if (calculateShippingCost) {
            dispatchParam.setCalculateShippingCost("否");
        } else {
            dispatchParam.setCalculateShippingCost("是");
        }
        dispatchParam.setConsultMoney("0");
        dispatchParam.setFreightMoney("0");
        dispatchParam.setDeptId(deptId);
        dispatchParam.setDeliverId(deliverId);
        tDispatchService.add(dispatchParam);
        return ResponseData.success();
    }

    @Override
    public List<TransportDispatchResult> findCarListBySpecYunShuZhongTooGetUnit(
            TransportDispatchParam transportDispatchParam, String dispatch, Long operateId) {
        return this.baseMapper.findCarListBySpecYunShuZhongTooGetUnit(transportDispatchParam, dispatch, operateId);
    }

    @Override
    public List<TransportDispatchResult> findLineListBySpec(TransportDispatchParam transportDispatchParam, String token,
                                                            Long operateId) {
        return this.baseMapper.findLineListBySpec(transportDispatchParam, token, operateId);
    }

    @Override
    public List<TransportDispatchResult> findLineListBySpecNew(TransportDispatchParam transportDispatchParam, String token,
                                                               Long operateId) {
        return this.baseMapper.findLineListBySpecNew(transportDispatchParam, token, operateId);
    }

    @Override
    public List<TransportDispatchResult> findLineGetUnitListBySpecNew(TransportDispatchParam transportDispatchParam, String token,
                                                                      Long operateId) {
        return this.baseMapper.findLineGetUnitListBySpecNew(transportDispatchParam, token, operateId);
    }

    @Override
    public List<TransportDispatchResult> findGetUnitListBySpec(TransportDispatchParam transportDispatchParam,
                                                               String token, Long operateId) {
        return this.baseMapper.findGetUnitListBySpec(transportDispatchParam, token, operateId);
    }

    @Override
    public Integer countGetUnit(TransportDispatchParam countppp) {
        return this.baseMapper.countGetUnit(countppp);
    }

    @Override
    public List<TransportDispatchResult> findSumGoodsBySpec(TransportDispatchParam transportDispatchParam) {
        return this.baseMapper.findSumGoodsBySpec(transportDispatchParam);
    }

    @Override
    public TransportDispatchResult findClZjSumBySpec(String dispatchNumber) {
        return this.baseMapper.findClZjSumBySpec(dispatchNumber);
    }

    @Override
    public List<TransportDispatchResult> findHxprintListBySpec(TransportDispatchParam transportDispatchParam) {
        return this.baseMapper.findHxprintListBySpec(transportDispatchParam);
    }


    @Override
    public List<TransportDispatchResult> findHxLmAssociationOrder(TransportDispatchParam transportDispatchParam) {
        return this.baseMapper.findHxLmAssociationOrder(transportDispatchParam);
    }

    @Override
    public TransportDispatchResult findClZjunitSumBySpec(String dispatchNumber, Long getLocationId) {
        return this.baseMapper.findClZjunitSumBySpec(dispatchNumber, getLocationId);
    }

    @Override
    public TransportDispatchResult findClZjunitSumPrintBySpec(String dispatchNumber, Long getLocationId) {
        return this.baseMapper.findClZjunitSumPrintBySpec(dispatchNumber, getLocationId);
    }

}
